CDOJ 1218 Pick The Sticks
Pick The Sticks
Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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 and output
| Sample Input | Sample Output |
|---|---|
4 3 7 |
Case #1: 2 |
Hint
In the third case, assume the container is lay on x-axis from 0 to 5. Xiu Yang could put the second gold stick center at 0 and put the third gold stick center at 5, so none of them will drop and he can get total 2+9=11 value. In the fourth case, Xiu Yang could just put the only gold stick center on any position of [0,1], and he can get the value of 3.
Source
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
LL dp[][][];
int a[maxn],v[maxn];
void read(int &tmp) {
char ch = getchar();
for (; ch>''||ch<''; ch=getchar());
tmp = ;
for (; ''<=ch&&ch<=''; ch=getchar())
tmp=tmp*+int(ch)-;
}
int main() {
int kase,N,L,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&N,&L);
L <<= ;
LL ret = ;
for(int i = ; i <= N; ++i) {
read(a[i]);
read(v[i]);
a[i] <<= ;
ret = max(ret,(LL)v[i]);
}
memset(dp,,sizeof dp);
int cur = ;
for(int i = ; i <= N; ++i) {
for(int j = L,x = a[i]>>; j >= ; --j) {
for(int k = ; k < ; ++k) {
dp[cur^][j][k] = dp[cur][j][k];
if(j >= x) {
if(a[i] <= j) dp[cur^][j][k] = max(dp[cur^][j][k],dp[cur][j-a[i]][k] + v[i]);
if(k) dp[cur^][j][k] = max(dp[cur^][j][k],dp[cur][j-x][k-] + v[i]);
}
ret = max(dp[cur^][j][k],ret);
}
}
cur ^= ;
}
printf("Case #%d: %lld\n",cs++,ret);
}
return ;
}
CDOJ 1218 Pick The Sticks的更多相关文章
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- UESTC 1218 Pick The Sticks
Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
Pick The Sticks Time Limit: 15000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- 2015南阳CCPC D - Pick The Sticks dp
D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...
- 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 ...
- hdu 5543 Pick The Sticks(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:给你一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上 ...
- [HDOJ5543]Pick The Sticks(DP,01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
随机推荐
- magento 自定义url路径 和 filter data 小结
背景是往一个第三方的搜索插件里面加入filter功能. 首先是路径,插件自己定义了一个router,类似于cms.那首先说说router好了,从入口一路追查的话,会发现最后进入的是Mage_Core_ ...
- 自定义view(13)自定义属性
1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ...
- iOS开发隐藏tabBar的问题
开发中遇到第一个页面需要显示tabBar,但是第二个页面不需要显示,当回到第一个页面的时候又需要显示的情况. 在第一个页面跳转到第二个页面的时候需要给第二个页面设置tabBar的隐藏 - (void) ...
- 常用的HTML5 pattern属性
type="tel" 和 type="number" 的区别 这里还是先那么先交代一下最初遇到的问题.其实无论是tel还是number都不是完美的: type= ...
- java 自定义注解,并使用示例
场景: 对需要校验 手机验证码和短信验证码的controller方法添加 自定义的注解 @CheckType 1. 定义注解 /** * 需要短信.验证码验证方法上的注解 * date: 2018年 ...
- AJPFX:不用递归巧妙求出1000的阶乘所有零和尾部零的个数
package com.jonkey.test; import java.math.BigInteger; public class Test6 { /*** @param args* 需求:求出1 ...
- client系列、offset系列、scroll系列
一.client系列 clientWidth/clientHeight 是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...
- 【学习笔记】深入理解js原型和闭包(1)—— 一切都是对象
“一切都是对象”这句话的重点在于如何去理解“对象”这个概念. ——当然,也不是所有的都是对象,值类型就不是对象. 首先咱们还是先看看javascript中一个常用的运算符——typeof.typeof ...
- ios 画板的使用
由于项目需求需要用到一个画板功能,需要这个画板可以实时的画,并且需要保存画板点集合从一端发送给另一端 达到一个实时同步的功能,前后使用了三种方法,每一种都遇到各种坑(后面会提到,每一种方法的优缺点), ...
- mongodb多条件查询总结
根据两字段乘积过滤查询分页数据 db.cron.aggregate([{$project:{_id:,AppID:,result:{$add:["$endlottery",&quo ...