您的位置:首页 > 其它

ZOJ 2059 The Twin Towers(dp)

2015-09-22 12:25 363 查看
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

Author: HU, Shichao

Source: ZOJ Monthly, December 2003

Submit

Status

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;
#define N 3005

int dp[105]
;
int n,a
;

void DP()
{
    int i,j;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for(int i=0;i<n;i++)
        for(int v=0;v<2005;v++)
        {
            dp[i+1][v]=max(dp[i+1][v],dp[i][v]);

            if(dp[i][v]<0) continue;

             if(v+a[i+1]<2005) dp[i+1][v+a[i+1]]=max(dp[i+1][v+a[i+1]],dp[i][v]+a[i+1]);

             if(a[i+1]<=v)
                dp[i+1][v-a[i+1]]=max(dp[i+1][v-a[i+1]],dp[i][v]);

             if(a[i+1]>=v)
                dp[i+1][a[i+1]-v]=max(dp[i+1][a[i+1]-v],dp[i][v]+a[i+1]-v);
        }
}

int main()
{
    int i,j;
    while(scanf("%d",&n)&&n>0)
    {
       for(int i=1;i<=n;i++)
          scanf("%d",&a[i]);
       DP();
       if(dp
[0]>0)
          printf("%d\n",dp
[0]);
       else
          printf("Sorry\n");
    }
    return 0;
}
/*

4 11 11 11 11
4 1 11 111 1111
-1

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: