双塔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的更多相关文章

  1. ZOJ 2059 The Twin Towers(双塔DP)

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  2. LightOJ1126 Building Twin Towers(DP)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...

  3. UVA 10066 The Twin Towers

    裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...

  4. UVA 10066 The Twin Towers(LCS)

    Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...

  5. uva 10066 The Twin Towers (最长公共子)

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  6. The Twin Towers zoj2059 DP

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  7. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  8. UVa 10192 - Vacation &amp; UVa 10066 The Twin Towers ( LCS 最长公共子串)

    链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...

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

随机推荐

  1. github入门操作

    一.更新github上的已有项目: 将repository clone到本地 shanyu@debian:~/Git$ git clone https://github.com/xunbu7/Hell ...

  2. CodeForces 687A NP-Hard Problem(二分图判定)

    这本来一个挺简单的题呢,结果让我给想复杂了,二分图就是把图分成了两部分,然后不同颜色各一边,肯定是满足题目中说的边和点的条件的,真是犯二了.. 代码如下: #include<iostream&g ...

  3. 河南多校大一训练赛 G 硬币

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125004#problem/G 密码:acm Description 宇航员Bob有一天来到火星上,他有收集硬币 ...

  4. 转:Jmeter 用户思考时间(User think time),定时器,和代理服务器(proxy server)

    在负载测试中需要考虑的的一个重要要素是思考时间(think time), 也就是在两次成功的访问请求之间的暂停时间. 有多种情形挥发导致延迟的发生: 用户需要时间阅读文字内容,或者填表,或者查找正确的 ...

  5. ice grid 第一篇

    关键点: (1)如果想提高并发的话,还是在一个node上对于同一个application开多个server instance. 1.名词解释 .Replica groups A replica gro ...

  6. TCP/IP模型各个层次的功能和协议

      层次名称 功    能 协    议 应用层 (Application Layer) 负责实现一切与应用程序相关的功能,对应OSI参考模型的上三层 FTP(文件传输协议) HTTP(超文本传输协议 ...

  7. css float left right 中间空间城数据无法显示

    css float left right 中间空间城数据无法显示 是由于设定了width具体值太小造成,简单用%值或不设置.

  8. 【抽屉定理】 组合数学poj2356

    假定n个数为a1,a2,...,an,前n项和分别是S1.S2.....Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在 1到n - 1之间,把余数作为抽屉,显然n个数放到n ...

  9. Strut2 spring hibernate 整合

    一.创建web项目工程 wzz 点击finish 2.添加spring Jar包   AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Da ...

  10. 原生 JS Ajax,GET和POST 请求实例代码

    javascript/js的ajax的GET请求代码如下所示: <script type="text/javascript"> /* 创建 XMLHttpRequest ...