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. lodash 工具库

    lodash是一套工具库,内部封装了很多字符串.数组.对象等常见数据类型的处理函数. 1.lodash的引用 import _ from 'lodash' 用一个数组遍历来说明为什么要使用lodash ...

  2. Photoshop - 描边

    描边在后期的UI制作的时候会比较少用,因为有一些缺陷,可以用选取收缩 1.快捷键E + S  (菜单栏-编辑-描边.photoshop cc) 2.使用图层样式进行描边(双击图层列表区域,图层缩略图的 ...

  3. Objective-C之成魔之路【19-归档】

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 在Obje ...

  4. ftp mybatis

    c# 字符串和Ascii码转换http://www.cnblogs.com/JoshuaDreaming/archive/2010/11/19/1882068.html ftp 公式 http://w ...

  5. lua面向对象编程 《lua程序设计》 16章 笔记

    Lua中的table就是一种对象,即它拥有状态.拥有独立于其值的标识(self).table与对象一样具有独立于创建者和创建地的征集周期 什么叫对象拥有独立的生命周期? Account = {bala ...

  6. lucene ParallelMultiSearcher与MultiSearcher的区别

    http://www.cnblogs.com/twilight/archive/2009/10/09/1579793.html ParallelMultiSearcher与MultiSearcher的 ...

  7. Xml帮助类

    public class XMLHelper { #region 将xml文件转换为object对象类型 /// <summary> /// 将xml文件转换为object对象类型 /// ...

  8. SpringCloud系列十:使用Feign实现声明式REST调用

    1. 回顾 前文的示例中是使用RestTemplate实现REST API调用的,代码大致如下: @GetMapping("/user/{id}") public User fin ...

  9. SpringCloud系列九:脱离Eureka使用Ribbon

    1. 回顾 在前文的示例中,是将Ribbon与Eureka配合使用的.但是现实中可能不具备这样的条件,例如一些遗留的微服务,它们可能并没有注册到Eureka Server上, 甚至根本不是使用Spri ...

  10. 10张思维导图带你学习【Java​Script】

    思维导图小tips:思维导图又叫心智图.是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的技巧.把各级主题的关系用相互隶属与相关的层级图表现出来 ...