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
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
Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3

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

The 2015 China Collegiate Programming Contest
 
解题:又一道dp,由于重心的计算可能会不能被2整除,可以选择将长度都乘以2,不影响结果
 #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的更多相关文章

  1. uestc oj 1218 Pick The Sticks (01背包变形)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...

  2. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  3. UESTC 1218 Pick The Sticks

    Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Status ...

  4. DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)

    题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...

  5. 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 ...

  6. 2015南阳CCPC D - Pick The Sticks dp

    D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...

  7. 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 ...

  8. hdu 5543 Pick The Sticks(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:给你一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上 ...

  9. [HDOJ5543]Pick The Sticks(DP,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...

随机推荐

  1. 人品问题 树形dp

    题目 网上出现了一种高科技产品——人品测试器.只要你把你的真实姓名输入进去,系统将自动输出你的人品指数.把儿不相信自己的人品为0.经过了许多研究后,把儿得出了一个更为科学的人品计算方法.这种方法的理论 ...

  2. URAL 7077 Little Zu Chongzhi's Triangles(14广州I)

    题目传送门 题意:有n根木棍,三根可能能够构成三角形,选出最多的三角形,问最大面积 分析:看到这个数据范围应该想到状压DP,这次我想到了.0010101的状态中,1表示第i根木棍选择,0表示没选,每一 ...

  3. magento layout xml 小结

    基础概念: http://magebase.com/magento-tutorials/demystifying-magentos-layout-xml-part-1/ 调试方案函数: $this-& ...

  4. 508 Most Frequent Subtree Sum 出现频率最高的子树和

    详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...

  5. 1051 - Good or Bad DFS 记忆化搜索

    http://lightoj.com/volume_showproblem.php?problem=1051 对于每个位置,设dfs(cur, one, two)表示前i个字母,拥有辅音字母one个, ...

  6. PoolManager插件(转载)

    http://www.xuanyusong.com/archives/2974 前几天我在博客里面分享了为什么Unity实例化很慢的原因,并且也分享了一个缓存池的工具.有朋友给我留言说PoolMana ...

  7. 在linux中使用多个redis端口来构建redis集群

    大家好,这是我制作的redis集群搭建视频教程. 服务器:ubnutu server(版本18.10) redis:redis-4.0.12 我这里就简单说明下大概步骤了,详细请观看教学视频. 首先更 ...

  8. asp.net 线程批量导入数据,ajax获取执行状态

    最近做了一个批量导入功能,长时间运行,没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能. 通过线程执行导入,并把正在执行的状态存入session,既共享执行状态,通过a ...

  9. 代码文件导到word里

    # 创建并写入word文档 import docx import sys import os dir = "D:\\gitwork\\fm.qimeng.c-class\\src\\main ...

  10. NIO服务端主要创建过程

    NIO服务端主要创建过程:   步骤一:打开ServerSocketChannel,用于监听客户端的连接,它是所有客户端连接的副管道,示例代码如下:      ServerSocketChannel ...