Http: // 202.121.199.212/judgeonline/problem. php? Cid = 1078 & pid = 7
Analysis: typical DP questions
DP [I] [J]: The height of the tall tower in the double tower where the height difference of the first crystal is J;
DP [I] [J] = d [I-1] [J], do not put the I crystal;
DP [I] [J] = f [I-1] [H [I]-J] + J, the I crystal is placed on a shorter tower, it turns a shorter tower into a higher one;
DP [I] [J] = f [I-1] [H [I] + J], the I crystal is placed on the shorter Tower, the shorter tower is still a shorter tower;
DP [I] [J] = f [I-1] [J-H [I] + H [I], the I crystal is placed on a high tower, the original high tower becomes a higher tower;
(You can draw a graph and the transfer equation will come out)
Code:
#include <iostream>#include <stdio.h>#include <string>#include <string.h>using namespace std; int h[120];int dp[120][2400]; int mmax(int a,int b){ return a<b ? b:a;} int main(){ //freopen("in.txt","r",stdin); int n; int sum=0; scanf("%d",&n); memset(dp, -1, sizeof(dp)); dp[0][0]=0; for(int i=1;i<=n;i++) {scanf("%d",&h[i]); sum +=h[i];} for(int i = 1; i <=n; i++) { for(int j = 0; j <= sum; j++) { if(dp[i-1][j]>-1) dp[i][j]=dp[i-1][j]; if(h[i]>j && dp[i-1][h[i]-j]>-1) dp[i][j]=mmax(dp[i][j],dp[i-1][h[i]-j]+j); if(j+h[i]<=sum && dp[i-1][j+h[i]]>-1) dp[i][j]=mmax(dp[i][j],dp[i-1][j+h[i]]); if(j>=h[i] && dp[i-1][j-h[i]]>-1) dp[i][j]=mmax(dp[i][j],dp[i-1][j-h[i]]+h[i]); } } if(dp[n][0] > 0) printf("%d\n",dp[n][0]); else printf("Impossible\n"); return 0;}