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个砖块,堆成两个塔的高度差,每个砖块有两个选择要么不用,要么放左边的塔,要么放右边的塔

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <stdio.h>
  7.  
  8. using namespace std;
  9. int a[105];
  10. int dp[105][4005];
  11. int n;
  12. int main()
  13. {
  14. while(scanf("%d",&n)!=EOF)
  15. {
  16. if(n<0)
  17. break;
  18. for(int i=1;i<=n;i++)
  19. scanf("%d",&a[i]);
  20. memset(dp,-1,sizeof(dp));
  21. dp[0][0+2000]=0;
  22. for(int i=1;i<=n;i++)
  23. {
  24. memcpy(dp[i],dp[i-1],sizeof(dp[i]));
  25. for(int j=-1999;j<=1999;j++)
  26. {
  27. if(dp[i-1][j+2000]==-1) continue;
  28. if(j<0)
  29. {
  30. dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+a[i]);
  31. dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+max(0,j+a[i]));
  32. }
  33. else
  34. {
  35. dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+a[i]);
  36. dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+max(0,a[i]-j));
  37. }
  38.  
  39. }
  40. }
  41. if(dp[n][2000]!=0&&dp[n][2000]!=-1)
  42. printf("%d\n",dp[n][2000]);
  43. else
  44. printf("Sorry\n");
  45. }
  46. return 0;
  47. }
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. puppeteer 相关知识

    page.waitForNavigation: 但我们通过代码执行到页面跳转时,我们需要等待跳转完成再作其他事情.使用page.waitForNavigation会等待跳转完成.(一般作用在点击链接或 ...

  2. ODOO Unable To Find Wkhtmltopdf On This System. Error/Bug ?

    If you are using ODOO version 8 and getting some error like – Unable to find Wkhtmltopdf on this sys ...

  3. Redis 配置文件及命令详解

    ==基本配置 daemonize no 是否以后台进程启动 databases 16 创建database的数量(默认选中的是database 0) save 900 1 #刷新快照到硬盘中,必须满足 ...

  4. <转>多线程中的lua同步问题

    转自 http://www.cnblogs.com/ghost240/p/3526185.html 最近写paintsnow::start时出现了一个非常麻烦的BUG,程序的Release版本大约每运 ...

  5. 【MyBatis学习07】动态sql

    1. 动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装.就拿上一篇博文中对用户的综合查询一例来 ...

  6. where 泛型类型参数及约束

    private void InsertData<TRowMetadata, TFieldMetadata, TCellMetadata>(IMetadataReader<TRowMe ...

  7. 常用sql语句记录

    1.表 --建表 if OBJECT_ID('Student') is not null create table Student( ID ,) not null, Name ), Code ), f ...

  8. POJ-3134-Power Calculus(迭代加深DFS)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  9. CSS实现子级窗口高度随低级窗口高度变化及js控制左右容器高度一致

    纯粹使用使用height:100%;或者height:auto;来定义内部容器自适应高度,都无法实现让内部容器高度随着外部父容器高度变化而变化,所以我们必需要使用position绝对定位属性来配合协助 ...

  10. docker mongo backup 不用找啦,就在这里。

    rm -rf /tmp/mongodump && mkdir /tmp/mongodumpdocker run -it --rm --link lps-mongodb:mongo -v ...