天天看點

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, 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[a][b] 第a個石頭,,兩塔的高度差。。不斷地去維護低塔。。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<24)
using namespace std;
int dp[][];
int a[];
int main()
{
    int n;
    while(scanf("%d",&n),n>)
    {
        int i,j;
        for(i=;i<=n;i++) scanf("%d",&a[i]);
        memset(dp,-,sizeof(dp));
        dp[][]=;
        for(i=;i<=n;i++)
        {
            memcpy(dp[i],dp[i-],sizeof(dp[i-]));//不放i,i-1的情況全是i的情況
            for(j=;j<;j++)  //高度
            {
                if(dp[i-][j]!=-)
                {
                    if(a[i]<j)
                    {
                        dp[i][j-a[i]]=max(dp[i][j-a[i]],dp[i-][j]+a[i]);//放到低塔上
                        dp[i][j+a[i]]=max(dp[i][j+a[i]],dp[i-][j]);//放到高塔上
                    }
                    else
                    {
                        dp[i][a[i]-j]=max(dp[i][a[i]-j],dp[i-][j]+j); //放到低塔上
                        dp[i][j+a[i]]=max(dp[i][j+a[i]],dp[i-][j]);//放到高塔上
                    }
                }
            }
        }
       if(dp[n][]) printf("%d\n",dp[n][]);
       else printf("Sorry\n");
    }
    return ;
}
           

後來又發現了可以再優化。。。。。

用滾動數組優化記憶體(雖然并沒有什麼卵用)(給自己提個醒,有這個用法吧)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<24)
using namespace std;
int dp[];  //dp[j]==高度相差j的矮塔的高度
int t[];   //滾動數組,記錄相當于dp[i]
int a[];
int main()
{
    int n;
    while(scanf("%d",&n),n>)
    {
        int i,j;
        for(i=;i<=n;i++) scanf("%d",&a[i]);
        memset(dp,-,sizeof(dp));
        memset(t,-,sizeof(t));
        dp[]=t[]=;
        for(i=;i<=n;i++)
        {
            memcpy(dp,t,sizeof(t));//不放i,i-1的情況全是i的情況
            for(j=;j<;j++)
            {
                if(dp[j]!=-)
                {
                    if(a[i]<j)
                    {
                        t[j-a[i]]=max(t[j-a[i]],dp[j]+a[i]);//放到低塔上
                        t[j+a[i]]=max(t[j+a[i]],dp[j]);//放到高塔上
                    }
                    else
                    {
                        t[a[i]-j]=max(t[a[i]-j],dp[j]+j); //放到低塔上
                        t[j+a[i]]=max(t[j+a[i]],dp[j]);//放到高塔上
                    }
                }
            }
            memcpy(dp,t,sizeof(t));//滾動數組,傳遞資料
        }
       if(dp[])    printf("%d\n",dp[]);
       else printf("Sorry\n");
    }
    return ;
}