ZOJ 2059 The Twin Towers(双塔DP)

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

时间: 2024-10-26 09:34:23

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

ZOJ 2059 The Twin Towers

双塔DP. dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int dp[105][4000 + 10]; int a[105]; int n, sum; void read() { for (int i = 1;

LightOJ1126 Building Twin Towers(DP)

题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Ali is fascinated about twin towers. So, in this problem you are working as his assistant, and you have to help him making a large twin towers. For this

zoj2059 The Twin Towers (dp)

Description 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, throug

uva--10066The Twin Towers +dp

其实就是求两个序列的最长公共子序列 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { int n1,n2,Case=0; while(scanf("%d%d",&n1,&n2)&&n1) { int a[110],b[110]; int dp[110][110],i,j; memse

UVa 10066 Twin Towers (DP 最长公共子序列)

题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=105; int a[maxn],b[maxn],d[maxn][maxn],na,nb; void lcs() { memset(d,0,sizeof(d)); for(int i=1; i<=na; ++i) for(in

uva 10066 The Twin Towers (最长公共子)

uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(&quo

uva 10066 The Twin Towers (最长公共子序列)

uva 10066 The Twin Towers 题目大意:最长公共子序列. 解题思路:最长公共子序列. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(

UVa 10066 The Twin Towers(最长公共子序列)

The Twin Towers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description Once upon a time, in an ancient Empire, there were two towers of dissimilar shapes in two different cities. The towers were built by putting circula

UVA 10066 The Twin Towers

DP,题目很长,题意就是求LCS. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<