双塔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. Math类的round方法小解

    在Math类中有三个关于“四舍五入”的静态方法(ceil,floor,round): ① Math.ceil(number) 向上取整,Math.ceil(11.2) 结果:12            ...

  2. eclipse Dynamic web project 工程目录

    如图,我创建了一个work 的web project,当工程完成之后,部署在服务器上时,整个work工程会被打包成一个war包,如 除了可以在eclipse上运行,工具会帮我们自动部署在服务器上之外, ...

  3. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  4. mongodb综述

    摘要:mongodb相对于mysql的优势在于性能和分布式扩展方面

  5. HTTP 中 POST和GET的区别

    1.传送数据的方式不一样 get是将数据队列添加到URL中提交,用户可以看到(对URL反编码就行) post是将数据队列放到HTTP的报文的报头中提交,用户看不到所提交的数据: 2.服务器端获取变量的 ...

  6. 【转】在 2016 年做 PHP 开发是一种什么样的体验?(一)

    原文: https://www.v2ex.com/t/312651 在 2016 年做 PHP 开发是一种什么样的体验?(一) 嘿,我最近接到一个网站开发的项目,不过老实说,我这两年没怎么接触编程,听 ...

  7. idx_rebuild_diff_idx_l.sql

    1. set echo on feedback on set timing on set time on set pagesize 1000 set linesize 150 spool 02_do_ ...

  8. DIV撑开

    引用自:http://radzhang.iteye.com/blog/1678734 在CSS排版中,如果一个层中的层使用了float浮动的话,那么就有可能会出现外层没有被内层撑开的情况,如以下代码所 ...

  9. map map

    下面的无法运行. @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Te ...

  10. Web工程师必备的43款可视化工具

    国外站点DATAVISUALIZATION.CH为大家总结出了当前热用的43款可视化工具,包括Arbor.Chroma.js.D3.js.Google Chart Tools等,绝对让你一饱眼福. 1 ...