http://acm.uestc.edu.cn/#/problem/show/1218

既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个。有一个trick就是如果只放一根,那么多长都可以。

wa了好多次(囧)

开始因为l[i]/2会出现小数,没注意,把所有的长度都x2就可以解决。

又wa了n次因为没注意j-l[i]时没加判断,为什么不是RE呢!不开心。。。

/*********************************************
Memory: 1140 KB Time: 3976 MS
Language: C++ Result: Accepted
*********************************************/
#include <iostream>
#include <cstring> using namespace std; typedef long long ll; int l[1005];
int v[1005];
ll dp[4005][5]; int main()
{
int t;
cin >> t;
int cas = 0;
while (t--)
{
cas++;
int n, L;
cin >> n >> L;
L *= 2;
ll ans = 0;
for (int i = 0; i < n; ++i)
{
cin >> l[i] >> v[i];
l[i] *= 2;
ans = max(ans, (ll)v[i]);
}
memset(dp, 0, sizeof dp);
for (int i = 0; i < n; ++i)
{
for (int j = L; j >= l[i] / 2; --j)
{
dp[j][2] = max(dp[j][2], dp[j - l[i] / 2][1] + v[i]);
if (j >= l[i]) dp[j][2] = max(dp[j][2], dp[j - l[i]][2] + v[i]);
dp[j][1] = max(dp[j][1], dp[j - l[i] / 2][0] + v[i]);
if (j >= l[i]) dp[j][1] = max(dp[j][1], dp[j - l[i]][1] + v[i]);
if (j >= l[i]) dp[j][0] = max(dp[j][0], dp[j - l[i]][0] + v[i]);
}
}
ans = max(ans, dp[L][2]);
cout << "Case #" << cas << ": " << ans << endl;
}
return 0;
} /**
Input:
50
2 3
1 2
2 3 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 Output:
5
2
6
11
3 */

  

2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)的更多相关文章

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

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

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

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

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

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

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

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

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

  6. CDOJ 1218 Pick The Sticks

    Pick The Sticks Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

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

  8. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  9. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

随机推荐

  1. 【filter】springmvc web.xml

    1.filter用于拦截用户请求,在服务器作出响应前,可以在拦截后修改request和response,这样实现很多开发者想得到的功能. 2.filter实现 ×编写一个继承Filter接口的类 ×在 ...

  2. CSS 元素透明

    1.HTML 元素透明 其实本身,CSS 实现元素透明是件容易事儿.直接上代码: opacity:.5 opacity 指的是不透明度,取值为 0~1 之间,1 表示完全不透明,0 表示完全透明. A ...

  3. [转载]C# Random 生成不重复随机数

    Random 类 命名空间:System 表示伪随机数生成器,一种能够产生满足某些随机性统计要求的数字序列的设备. 伪随机数是以相同的概率从一组有限的数字中选取的.所选数字并不具有完全的随机性,因为它 ...

  4. How to steal any developer's local database

    原文链接: http://bouk.co/blog/hacking-developers/ If you’re reading this and you’re a software developer ...

  5. android Failure [INSTALL_FAILED_OLDER_SDK] 安装apk失败

    安装文件与运行环境的skd不匹配 打开源码目录下的AndroidManifest.xml文件,然后注释掉或者删除掉这行: <uses-sdk android:minSdkVersion=&quo ...

  6. 2、RenderScript的计算(2013.05.07)

    一.官方文档翻译: Android Renderscript计算 参考网址: http://blog.csdn.net/fireofstar/article/details/7748143 http: ...

  7. jni.h源码

    /* * jni.h * Java Native Interface. * * Copyright (c) 1996, 1997 * Transvirtual Technologies, Inc. A ...

  8. Android 内核初识(6)SystemServer进程

    简介 SystemServer的进程名实际上叫做“system_server”,通常简称为SS. 系统中的服务驻留在其中,常见的比如WindowManagerServer(Wms).ActivityM ...

  9. 应付分配集 Distribution Sets

    (N) AP > Setup > Invoice > Distribution Sets (定义分配集) You can use a Distribution Set to auto ...

  10. 在Sublime Text 3中配置编译和运行C++程序

    下载解压MinGW至目标目录,本次安装的解压目录为C:\MinGW 设置环境变量.右击我的电脑,属性-->高级-->环境变量. 在系统环境变量PATH里添加C:\MinGW\bin(如果里 ...