"Problem description"
A sudden disaster destroyed the World Trade Center building in New York on September 11, 2001, and Mr F had witnessed the disaster. To commemorate the "9?11" incident, Mr F decided to use the crystal to build a twin tower.
Mr F has n pieces of crystal, each crystal has a height, he wants to use this n pieces of crystal to build two towers with the same height, so that they become a twin towers, Mr F can be from the N-block crystal to take M (1≤M≤N) block to build. But he did not know whether the towers would be of the same height, or what the maximum height of the Twin towers would be if they could build a twin tower. So he came to ask you for help.
Given the number of crystals N and the height of each crystal Hi, your task is to judge Mr. F can use these crystals to build a twin towers (two towers have the same height), if you can, the output can be built the maximum height of the Twin towers, otherwise output "impossible".
"Input Format"
The first act a number n, which represents the number of crystals.
The second behavior n number, the number of I represents the height of the first crystal.
"Output Format"
The output contains only one row, if it can be built into a twin towers, output the maximum height of the Twin towers, otherwise output a string "impossible".
"Input Sample"
5
1 3 4 5 2
"Output Sample"
7
"Data Range"
50% data: 1≤n≤20, N block crystal height of the sum of not more than 2000;
70% data: 1≤n≤100, N block crystal height of the sum of not more than 2000;
100% data: 1≤n≤100, N block crystal height of the sum of not more than 500000.
The idea of doing a problem (70 decomposition method): When you get this problem, because of the Twin Towers (two tower height of the same) the maximum height, so think of recursion algorithm, according to what to set what, F (i,j,k) that the first block of crystal can build a height of J, the second tower height of K, Since the analysis of the former block crystal is only related to the former i-1 block crystal, you can use a scrolling array to omit one dimension. The analysis of Block I crystal, can not lap, can be built on the first tower, can also be built on the second tower, so the recursive equation (scrolling array) is: F (j,k) =f (j,k) | | F (j-h[i],k) | | F (J,k-h[i]) (that is, if there is one viable in F (j,k), F (j-h[i],k), F (j,k-h[i)), F (j,k) is also feasible. The boundary condition is f (0,0) = 1, that is, no matter how many pieces of crystal, can choose not to match, at this time the height of the two towers are 0. But if you set the state function, according to the data range of the topic, n block crystal height sum not more than 500000, if set f[500005][500005] obviously will exceed memory (limit 128M), so for the sake of insurance, the examination only set f[2005][2005].
Problem solving ideas (positive solution): Because the known if directly to what set what is not, so to change the idea to design the state function, because the two towers are the same height, that is, the height difference is 0, so you can use dynamic planning , set F (i,j) to represent the former I block Crystal, Select some of the height difference between the first tower and the second tower is J, the maximum height of the first tower, this way, the array only need to set f[105][500005], does not exceed the memory, but you can find the height difference J may be negative, for this, you can use the macro definition to translate negative j to positive numbers. Similarly, the analysis of block I crystals, can not be used, can be built on the first tower, can also be used on the second tower, so the state transition equation is f (i,j) =max (f (i-1,j), F (i-1,j-h[i)), F (i-1,j+h[i)) +h[i]). The boundary condition is f (0,0) = 0, that is, the crystal is not selected, the height difference between the two Towers is 0, the tower height is also 0. It is to be noted that, because of this attempt to analyze the former block crystal, still only with the former i-1 block crystal, so you can use a scrolling array to optimize the space, but in order to ensure correct, you can use two-dimensional scrolling array, in the enumeration height difference J, can be optimized, first in the input to calculate the first block crystal height and sum[i] (prefix and), the height difference is at most sum[i], at least-sum[i, when the block I crystal is enumerated. If the last F (n%2,0) is less than or equal to 0, there is no twin towers, the output "impossible" is required.
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <
cstring> #define F (x,y) d[(x) [(y) +1000000]//macro definition, note that the proposed translation is a bit more, otherwise the possible data may have problems using namespace std;
const int maxn=105;
const int inf=1000000010;
int n,sum=0;
int H[MAXN],S[MAXN]; /* F (I,J) represents the former I block crystal, select some of the first tower and the second tower when the height difference is J, the maximum height of the first tower F (i,j) =max (f (i-1,j), F (i-1,j-h[i)), F (i-1,j+h[i)) +h[i]) boundary: F (0,0) =0 */int d[2][2000005]; Two-dimensional scrolling array void solve ()//Dynamic planning {for (int i=0;i<2;i++) for (int j=-sum;j<=sum;j++) f (i,j) =-inf; Initialize f (0,0) = 0;
Boundary for (int i=1;i<=n;i++) {for (int j=-s[i];j<=s[i];j++) f (i%2,j) =-inf;
for (int j=-s[i];j<=s[i];j++) {int t1=f ((i-1)%2,j), T2=f ((i-1)%2,j-h[i]), T3=f ((i-1)%2,j+h[i]) +h[i];
F (i%2,j) =max (T1,max (T2,T3));
} if (f (n%2,0) >0) printf ("%d\n", F (n%2,0));
else printf ("impossible\n");
/*for (int j=-s[n];j<=s[n];j++) printf ("%d", f (n%2,j)),/* int main () {freopen ("tower.in", "R", stdin); Freopen ("Tower1.out", "w", StdoUT);
scanf ("%d", &n);
s[0]=0;
for (int i=1;i<=n;i++) {scanf ("%d", &h[i]); S[i]=s[i-1]+h[i]; preprocessing, calculating prefixes and sum+=h[i];
Calculate the range of height difference} solve ();
return 0; }
After the examination: in the exam, if really can't think out the procedure, then must ensure that part of the program is correct (not because the scope of the topic is too large, blindly set too large, may be super memory).