双塔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. VHD进阶:差分VHD备份系统

    VHD进阶:差分VHD备份系统 一.创建虚拟磁盘 方法1:图形界面创建 1.打开磁盘管理器(运行diskmgmt.msc),在“磁盘管理”上点击右键,“创建VHD”,类型选择VHD,动态扩展或者固定大 ...

  2. java 参数传值

    基本数据类型参数的传值,参数为基本数据类型 class Computer{ int add(int x,int y){ return x+y; } } public class Example4_6 ...

  3. elasticsearch高级配置之(二)----线程池设置

    elasticsearch 配置 线程池  一个Elasticsearch节点会有多个线程池,但重要的是下面四个:  索引(index):主要是索引数据和删除数据操作(默认是cached类型)  搜索 ...

  4. mysql 查询 45 道题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  5. linux 命令实现原理

    我们知道有些Linux的命令涉及到一些高效率的算法,在此做出一个积累吧,不是系统的. 1.tail命令打印一个文件的最后num行 2.grep命令从文本中匹配字符串 基于正则表达式的匹配很快. it ...

  6. asp 自我定时删除

    <% if now()>"2008-9-15" thenset myfso=server.CreateObject("scripting.filesystem ...

  7. Android系统权限及签名

    Android系统权限及签名   2015-03-23 19:13:33CSDN-chen52671-点击数:50     Android权限及签名 引子 现象:系统中的一个定制Service,服务是 ...

  8. oracle数据库兼容mysql的差异写法

    1.sysdate改为sysdate(),或者now(); 2.nvl(expr1,expr2) 改为IFNULL(expr1,expr2) nvl2(expr1,expr2,expr3)改为 IF( ...

  9. android Service Activity三种交互方式(付源码)(转)

    android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread  android Service Binder交互通信实例 最下边有源代码: ...

  10. 转:web_custom_request 函数

    语法:Int web_custom_request (const char *RequestName, <List of Attributes>, [EXTRARES, <List ...