-
Title Description:
-
Tino wrote a long long story. but! In Chinese ...
So I has the to tell you the problem directly and discard he long long story. That's Tino want to carry some oranges with "carrying pole", and he must make-side of the carrying pole are the same Weight. Each orange has its ' weight. So greedy Tino want to know the maximum weight he can carry.
-
input:
-
the first line of input contains a number t, which means there is t cases of the test data.
The first line contain a number n, indicate the number of oranges.
The second line contains n numbers, Wi, indicate the weight of all orange
N is between 1 and inclusive. Wi is between 0 and inclusive. The sum of Wi is equal or less than.
-
Output:
-
For each test case, output the maximum weight in one side of carrying pole. If you can ' t carry any orange, output-1. Output format is shown in Sample output.
-
Sample input:
-
151 2 3) 4 5
-
-
Sample output:
-
-
Case 1:7
-
-
The dynamic programming algorithm solves the transfer equation as follows:
-
-
The equation means: Select in the first I items, divided into two piles, the first heap than the second pile of heavy J kg, the maximum total weight of the two piles combined! (Note: J can be negative)
-
-
The code is as follows:
-
#include <stdio.h> #include <string.h> #define OFFSET 2000//because J has positive or negative, so add an offset to make the title # define INF 0x7fffffffint Wi[101];int dp[101][4001];int Max (int A, int b, int c) { int flag = a; if (flag < b) { &NB Sp flag = b; } if (flag < C) { flag = c; }&N Bsp return flag;} void Init () { for (int i=0;i<40001;i++) Dp[0][i] =-inf; &nbs P Dp[0][offset] = 0;//core}int main () { int T, n; scanf ("%d", &t); for (int i = 0; I < T; i++) { scanf ("%d", &n); wi[0] = 0; &NB Sp;bool Haszero = false; int cnt = 0; for (int j = 0; J < N; j + +) { scanf ("%d", &wi[++cnt]); &NBSP if (wi[cnt] = = 0) { haszero = true; cnt--; } &NBS P } n = cnt; init (); for (int j = 1; J <= N J + +) { for (int k = -2000; k <=; k++) { &N Bsp Dp[j][k+offset] = max (Dp[j-1][k-wi[j]+offset]+wi[j], dp[j-1][k+wi[j]+offset]+wi[j], Dp[j-1][k+offset]) ; } } printf ("Case%d:", i+1); if (Dp[n][0+offset] = 0) { if (Haszero = True) {&N Bsp printf ("0\n"); }else{ &nbs P printf (" -1\n"); } } else{ printf ("%d\n", DP[N][0+OFFSET]/2); }   ; } return 0;}
-
-
Nine degree topic 1453:greedy Tino