Description
Twin Towers We see your standing tall, though a building ' s lost our faith would never fall.
Twin Towers The world hears your call, though your ' re gone it only strengthens our resolve.
We couldn ' t make it through this without your 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 one, 2001, Five-year-old Rosie Decids to rebuild a Tallest Twin Towers by using the crystals her brother have collected for years. Would she succeed in building the towers of the same height?
Input
There is mutiple test cases.
One line forms a-test case. The first integer n (n < a) tells you the number of crystals she 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 is in cube shape. The total height of crystals is smaller than 2000.
Output
If It is impossible, you would say "Sorry" and 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
Give you something of a height. Then ask you to use these can make up two height of the same tower.
Dp[a][b] The first stone, the height difference between the two towers. Constantly to maintain the low tower.
#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;intdp[ the][2005];inta[ the];intMain () {intN while(scanf("%d", &n),n>0) {intI,j; for(i=1; i<=n;i++)scanf("%d", &a[i]);memset(dp,-1,sizeof(DP)); dp[0][0]=0; for(i=1; i<=n;i++) {memcpy(dp[i],dp[i-1],sizeof(dp[i-1]));//Do not put the i,i-1 situation is all the case of I for(j=0;j<2001; j + +)//Height{if(dp[i-1][j]!=-1) {if(A[I]<J) {Dp[i][j-a[i]]=max (dp[i][j-a[i]],dp[i-1][j]+a[i]);//Put it on the low towerDp[i][j+a[i]]=max (dp[i][j+a[i]],dp[i-1][J]);//Put it on the high tower}Else{Dp[i][a[i]-j]=max (dp[i][a[i]-j],dp[i-1][J]+J);//Put it on the low towerDp[i][j+a[i]]=max (dp[i][j+a[i]],dp[i-1][J]);//Put it on the high tower} } } }if(dp[n][0])printf("%d\n", dp[n][0]);Else printf("sorry\n"); }return 0;}
Later found can be re-optimized .....
Use a scrolling array to optimize memory (although there is no egg) (Give yourself a wake-up call, have this usage)
#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;intdp[2005];height of//dp[j]== height-difference J Dwarf Towerintt[2005];//scroll array, record equivalent to Dp[i]inta[ the];intMain () {intN while(scanf("%d", &n),n>0) {intI,j; for(i=1; i<=n;i++)scanf("%d", &a[i]);memset(dp,-1,sizeof(DP));memset(t,-1,sizeof(t)); dp[0]=t[0]=0; for(i=1; i<=n;i++) {memcpy(Dp,t,sizeof(t));//Do not put the i,i-1 situation is all the case of I for(j=0;j<2001; j + +) {if(dp[j]!=-1) {if(A[I]<J) {T[j-a[i]]=max (t[j-a[i]],dp[j]+a[i]);//Put it on the low towerT[j+a[i]]=max (T[j+a[i]],dp[j]);//Put it on the high tower}Else{T[a[i]-j]=max (T[A[I]-J],DP[J]+J);//Put it on the low towerT[j+a[i]]=max (T[j+a[i]],dp[j]);//Put it on the high tower} } }memcpy(Dp,t,sizeof(t));//scrolling array, passing data}if(dp[0])printf("%d\n", dp[0]);Else printf("sorry\n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
zoj2059 the Twin Towers (DP)