ZOJ 2059 The Twin Towers
双塔DP。
dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int dp[][ + ];
int a[];
int n, sum; void read()
{
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
} void work()
{
sum = ;
for (int i = ; i <= n; i++) sum = sum + a[i];
memset(dp, -, sizeof dp);
int h; h = a[];
dp[][h + sum] = dp[][ - h + sum] = dp[][sum] = ;
for (int i = ; i <= n; i++)
{
h = a[i];
for (int j = ; j <= sum*; j++) dp[i][j] = dp[i - ][j];
for (int j = ; j <= sum*; j++)
{
if (dp[i - ][j] == -) continue; int tmp = j - sum;
if (tmp >= )
{
dp[i][h + tmp + sum] = max(dp[i][h + tmp + sum], dp[i - ][j]);
dp[i][tmp - h + sum] = max(dp[i][tmp - h + sum], dp[i - ][j] + min(tmp, h));
}
else if (tmp<)
{
tmp = -tmp;
dp[i][ - (tmp + h) + sum] = max(dp[i][ - (tmp + h) + sum], dp[i - ][j]);
dp[i][h - tmp + sum] = max(dp[i][h - tmp + sum], dp[i - ][j] + min(tmp, h));
}
}
}
int ans = ;
for (int i = ; i <= n; i++) ans = max(ans, dp[i][sum]);
if (ans == ) printf("Sorry\n");
else printf("%d\n", ans);
} int main()
{
while (~scanf("%d", &n))
{
read();
if (n < ) break;
if (n <=) printf("Sorry\n");
else work();
}
return ;
}
ZOJ 2059 The Twin Towers的更多相关文章
- ZOJ 2059 The Twin Towers(双塔DP)
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- UVA 10066 The Twin Towers
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- UVA 10066 The Twin Towers(LCS)
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...
- uva 10066 The Twin Towers (最长公共子)
uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...
- The Twin Towers zoj2059 DP
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...
- zoj 4099 Extended Twin Composite Number
Do you know the twin prime conjecture? Two primes and are called twin primes if . The twin prime c ...
随机推荐
- 使用devcon禁用启用网卡
系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...
- GameUnity 2.0 文档(四) 网格+四叉树 最优碰撞检测
在写之前,必须对 前一篇文档补充一下. Camera2DAngle类是 摄像机旋转 2d人物360度,PlayerMove是人物移动. 这两个类 都可以 360 ° 场景旋转人物也跟着旋转. 但不能 ...
- PARTITION BY 和 group by
sum() over (PARTITION BY ...) 是一个分析函数. 他执行的效果跟普通的sum ...group by ...不一样,它计算组中表达式的累 ...
- Video Pooling
Video pooling computes video representation over the whole video by pooling all the descriptors from ...
- NPOI 2.0 教程(二):编辑既存的EXCEL文件
NPOI 2.0 教程(二):编辑既存的EXCEL文件 分类: C#技术 2014-03-11 15:40 993人阅读 评论(3) 收藏 举报 c#excelNPOI 转载请注明出处 http:// ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- Eclipse/MyEclipse 最最常用的快捷键
F 键类 F2 显示详细信息 F3 跳到声明或定义的地方 Ctrl + 键类 Ctrl+1 快速修复 ( 最经典的快捷键 , 就不用多说了 ) Ctrl+D 删除当前行 Ctrl+E 快速显示当前 E ...
- copy ,abs,includes 3个函数
copy: Copy函数原型:标头: <algorithm> OutputIterator copy( InputIterator begin, InputIterator end, o ...
- Eclipse开发Maven项目提示:程序包org.junit不存在解决方案
原因: 个人考虑产生此错误的原因是因为Eclipse中对于测试和开发的鉴定不明?Intellij中没有错误,因为Intellij对项目的管理就是同Maven结构的. 解决方案: 原来的junit的sc ...
- html中DIv并排显示问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...