The Twin Towers


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Twin towers we see you standing tall, though a building's lost our faith will never fall.

Twin towers the world hears your call, though you're gone it only strengthens our resolve.

We couldn't make it through this without you Lord, through hard times we come together more. ... 



Twin Towers - A Song for America



In memory of the tragic events that unfolded on the morning of September 11, 2001, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has
collected for years. Will she succeed in building the two towers of the same height?





Input



There are mutiple test cases.

One line forms a test case. The first integer N (N < 100) tells you the number of crystals her brother has collected. Then each of the next N integers describs the height of a certain
crystal.

A negtive N indicats the end.

Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.

Output



If it is impossible, you would say "Sorry", otherwise tell her the height of the Twin Towers.

Sample Input



4 11 11 11 11

4 1 11 111 1111 

-1

Sample Output



22

Sorry

双塔DP

dp[i][2000] 表示第i个砖块,堆成两个塔的高度差,每个砖块有两个选择要么不用,要么放左边的塔,要么放右边的塔

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int a[105];
int dp[105][4005];
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n<0)
break;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));
dp[0][0+2000]=0;
for(int i=1;i<=n;i++)
{
memcpy(dp[i],dp[i-1],sizeof(dp[i]));
for(int j=-1999;j<=1999;j++)
{
if(dp[i-1][j+2000]==-1) continue;
if(j<0)
{
dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+a[i]);
dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+max(0,j+a[i]));
}
else
{
dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+a[i]);
dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+max(0,a[i]-j));
} }
}
if(dp[n][2000]!=0&&dp[n][2000]!=-1)
printf("%d\n",dp[n][2000]);
else
printf("Sorry\n");
}
return 0;
}
The Twin Towers


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Twin towers we see you standing tall, though a building's lost our faith will never fall.

Twin towers the world hears your call, though you're gone it only strengthens our resolve.

We couldn't make it through this without you Lord, through hard times we come together more. ... 



Twin Towers - A Song for America



In memory of the tragic events that unfolded on the morning of September 11, 2001, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has
collected for years. Will she succeed in building the two towers of the same height?





Input



There are mutiple test cases.

One line forms a test case. The first integer N (N < 100) tells you the number of crystals her brother has collected. Then each of the next N integers describs the height of a certain
crystal.

A negtive N indicats the end.

Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.

Output



If it is impossible, you would say "Sorry", otherwise tell her the height of the Twin Towers.

Sample Input



4 11 11 11 11

4 1 11 111 1111 

-1

Sample Output



22

Sorry

ZOJ 2059 The Twin Towers(双塔DP)的更多相关文章

  1. ZOJ 2059 The Twin Towers

    双塔DP. dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]. #include<cstdio> #include<cst ...

  2. LightOJ1126 Building Twin Towers(DP)

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

  3. The Twin Towers zoj2059 DP

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

  4. ZOJ 3331 Process the Tasks 双塔Dp

    用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间.然后根据j的正负,分别考 ...

  5. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

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

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

  7. ZOJ 3331 Process the Tasks(双塔DP)

    Process the Tasks Time Limit: 1 Second      Memory Limit: 32768 KB There are two machines A and B. T ...

  8. UVA 10066 The Twin Towers

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

  9. UVA 10066 The Twin Towers(LCS)

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

随机推荐

  1. Odoo8中“更多”下拉菜单选项指定后台执行代码

    在Odoo8中的仓库模块,根据每日最小安全库存数量,系统会自动生成一些补货单,而且是一个产品会生成一笔,如果产品比较多,这里生成的补货单也会很多. 如果这里的补货单没有即时处理,那相同产品后续不会再生 ...

  2. (转)C的代码是如何变成程序的

    原文链接:http://blog.csdn.net/fz_ywj/article/details/8769825 C语言是一门典型的编译语言,源代码文件需要编译成目标代码文件才能运行.可以认为程序文件 ...

  3. XAOP的使用示范例子

    代码地址如下:http://www.demodashi.com/demo/12976.html XAOP 一个简易的AOP(Android)应用框架.囊括了最实用的AOP应用. 特点 支持快速点击切片 ...

  4. WP8控件进度条(ProgressBar)的使用

    --前台代码 <Grid x:Name="ContentPanel"  Grid.Row="1" Margin="0,0,24,0"& ...

  5. iOS开发-关闭/收起键盘方法总结

    前言:作为IOS开发人员,需要经常和表单打交道.因此我对收起键盘的方法作了下总结,IOS收起键盘有三种方法(如果有其它收起键盘的方法请在留言区指错). 收起键盘的方法: 1.点击Return按扭时收起 ...

  6. java类反射

    http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

  7. MATLAB 的运算符

    在MATLAB中,提供了丰富的运算符,运算主要包括算数运算.关系运算和逻辑运算. 一.算数运算符 分为标量和数组运算和矩阵运算.需要注意:对于a/b,是a除以b,对于a\b,是b除以a.在MATLAB ...

  8. js获取100个随机数存入数组

    . //js获取100个随机数存入数组 $(function () { var arr = []; ; var str = ""; ) { , ); ) { arr[num] = ...

  9. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)(转)

    转自:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详 ...

  10. Linux----LVM扩容磁盘空间(讲的也很好)

    转:https://www.cnblogs.com/tail-f/p/6143085.html