D - Pick The Sticks

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

Description

The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting idea and enjoyed it.

Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because he's clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.

He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.

Formally, we can treat the container stick as an L length segment. And the gold sticks as segments too. There were many gold sticks with different length ai and value vi. Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.

As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.

Can you help solve the mystery by finding out what's the maximum value of the gold sticks Xiu Yang could have taken?

Input

The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Each test case start with two integers, N(1≤N≤1000) and L(1≤L≤2000), represents the number of gold sticks and the length of the container stick. N lines follow. Each line consist of two integers, ai(1≤ai≤2000) and vi(1≤vi≤109), represents the length and the value of the ith gold stick.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum value of the gold sticks Xiu Yang could have taken.

Sample Input

4

3 7
4 1
2 1
8 1 3 7
4 2
2 1
8 4 3 5
4 1
2 2
8 9 1 1
10 3

Sample Output

Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3

HINT

题意

给你一个长度为l的盒子,然后里面可以放置一排金砖,只要这个金砖的重心在盒子里面就好了

然后问你放置的金砖的最大价值是多少

题解:

不能伸出去,这就是一个01背包

然后我们贪心一下,我们肯定是选择两个金砖砍一半扔边上,然后剩下就是简单的01背包了

于是我们就dp[i][j]表示空间为i,有j个砍了一半扔在了边上的最大价值是多少

然后暴力转移就好了

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std; long long dp[][][];
struct node
{
int x;
long long y;
};
node p[];
int main()
{
int t;scanf("%d",&t);
for(int cas = ; cas <= t; cas++)
{
memset(dp,,sizeof(dp));
memset(p,,sizeof(p));
int n,l;scanf("%d%d",&n,&l);
long long ans = ;
for(int i=;i<=n;i++)
{
scanf("%d%lld",&p[i].x,&p[i].y);
p[i].x*=;
ans = max(ans,p[i].y);
}
l *= ;
int now = ;
for(int i=;i<=n;i++)
{
now = -now;
for(int j=;j<=l;j++)
for(int k=;k<;k++)
dp[now][j][k]=dp[-now][j][k];
for(int j=l;j>=p[i].x/;j--)
{
for(int k=;k<;k++)
{
if(j>=p[i].x)dp[now][j][k]=max(dp[now][j][k],dp[-now][j-p[i].x][k]+p[i].y);
if(k)dp[now][j][k]=max(dp[now][j][k],dp[-now][j-p[i].x/][k-]+p[i].y);
}
}
}
for(int t=;t<;t++)
for(int i=;i<=l;i++)
for(int k=;k<;k++)
ans = max(dp[t][i][k],ans);
printf("Case #%d: %lld\n",cas,ans);
}
}

2015南阳CCPC D - Pick The Sticks dp的更多相关文章

  1. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

  2. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  3. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

  4. 2015 南阳ccpc The Battle of Chibi (uestc 1217)

    题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...

  5. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  6. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  7. 2015南阳CCPC L - Huatuo's Medicine 水题

    L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...

  8. 2015南阳CCPC H - Sudoku 暴力

    H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...

  9. 2015南阳CCPC G - Ancient Go 暴力

    G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...

随机推荐

  1. Ruby基础数据类型

    #数字分为证书Integer,浮点数Float(对应与其他语言中的double),和复数Complex #整数又分为Fixnum和Bignum,Fixnum和Bignum会互相转换,这些都是ruby自 ...

  2. css的框架——base.css

    一.常用的base.css文件(也是比较简略的,但按需增加) body,ul,li,ol,dl,dd,h1,h2,h3,h4,h5,h6,input,p{ margin:;} ul,ol { padd ...

  3. PHP的GD库函数大全

    GetImageSize作用:取得图片的大小[即长与宽]  用法:array GetImageSize(string filename, array [imageinfo]); ImageArc作用: ...

  4. ch03-文字版面的设计

    Ch03: 文字版面的编辑 3.1 版面控制标记 3.1.1 取消文字换行: <NOBR> 1-取消换行文字实例:1-取消换行文字实例; 1-取消换行文字实例; 2-取消换行文字实例:2- ...

  5. 你所不知道的 URL

    0.说明 第一幕 产品:大叔有用户反映账户不能绑定公众号.大叔:啊咧咧?怎么可能,我看看?大叔:恩?这也没问题啊,魏虾米.大叔:还是没问题啊,挖叉类.大叔:T T,话说产品姐姐是不是Java提供接口的 ...

  6. 《Python基础教程(第二版)》学习笔记 -> 第十章 充电时刻 之 标准库

    SYS sys这个模块让你能够访问与Python解释器联系紧密的变量和函数,下面是一些sys模块中重要的函数和变量: 函数和变量 描述 argv 命令行参数,包括脚本和名称 exit([arg])   ...

  7. C语言反转字符串

    也是面腾讯的一道编程题=,= 这题比较简单 代码如下: #include <stdio.h> #include <string.h> // 非递归实现字符串反转 char *r ...

  8. 树-哈夫曼树(Huffman Tree)

    概述 哈夫曼树:树的带权路径长度达到最小. 构造规则 1. 将w1.w2.-,wn看成是有n 棵树的森林(每棵树仅有一个结点): 2. 在森林中选出根结点的权值最小的两棵树进行合并,作为一棵新树的左. ...

  9. VSim [a Racing-simulator by Vell001]

    VSim [a racing-simulator by vell001] This is my first project about Racing. I am a Chinese with bad ...

  10. [Hive - LanguageManual] Alter Table/Partition/Column

    Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add ...