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的线段上,要么有不超过自身长度一半的部分 ...
随机推荐
- 1-3方法的重载(overload)
之前已经写了一个方法sumInt用来计算两个int类型数字的和,如果要是想计算两个float类型数字的和呢?ok,那就再来写一个sumFloat方法,除此之外,还有long类型,double类型,如果 ...
- SPRING-BOOT系列之Spring4深入分析
上篇 : SPRING-BOOT系列之Spring4快速入门 1. 假如我们有这样一个场景,在一个组件中想获取到容器对象,那么我们也可以使用Autowired来完成装配.那么我们还可以让类集成一个接口 ...
- [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
本文转自:http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NE ...
- vscode中将本地数据push至git repository
1.新建repository 2.本地写好的代码 3.执行git init 初始化git配置文件 4.提交已暂存文件 5.填写提交信息 6.执行push命令 7.完成
- Python 版本对比
python2 与 python3可认为代码不通用,你也可以点击Python2.x与3.x版本区别来查看两者的不同 python3.6以上支持f-string,一种很方便的变量替换方式 高版本可能 ...
- 前端之HTML样式
<!doctype html> h5的文档声明 <html> 网页的根标签(根元素 html)--所有的代码都放置在此内 <head> <meta chars ...
- T4312 最大出栈顺序
题目描述 给你一个栈和n个数,按照n个数的顺序入栈,你可以选择在任何时候将数 出栈,使得出栈的序列的字典序最大. 输入输出格式 输入格式: 输入共2行. 第一行个整数n,表示入栈序列长度. 第二行包含 ...
- obj.style 和currentstyle 等区别
版权声明:本文为博主原创文章,未经博主允许不得转载. 获取样式 obj.style 和currentstyle 等区别 obj.style只能获得内嵌样式(inline Style)就是写 ...
- Hibernate核心接口和工作原理
Hibernate核心接口和工作原理 Hibernate有五大核心接口,分别是:Session .Transaction .Query .SessionFactory .Configuration . ...
- BST AVL RBT B- B+ 的一些理解
BST(二叉查找树,排序二叉树),如果数据有序的话,组成的二叉树会形成单列的形式,导致查询效率低AVL(平衡二叉树) 使树的左右高度差的绝对值不超过2,保证了查询效率.但是插入和删除会带来多次旋转,导 ...