时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:383
解决:121
-
题目描述:
-
Tino wrote a long long story. BUT! in Chinese...
So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with "Carrying pole", and he must make two side of the Carrying pole are the same weight. Each orange have its' weight. So greedy tino want to know the maximum weight he can carry.
-
输入:
-
The first line of input contains a number t, which means there are t cases of the test data.
for each test case, the first line contain a number n, indicate the number of oranges.
the second line contains n numbers, Wi, indicate the weight of each orange
n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.
-
输出:
-
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.
-
样例输入:
-
1 5 1 2 3 4 5
-
样例输出:
-
Case 1: 7
//Greedy Tino
#include<cstdio>
#include<cstring>
#define INF 0x7FFFFFFF
#define MAX(a,b) (a)>(b)?(a):(b)
const int MAX = 101;
const int DIS = 4000;
const int OFFSET = 2000;
int w[MAX];//weight of each
int dp[MAX+1][DIS+1];//first weithter than second,the whold weight is.
void init()
{
memset(w,0,sizeof(w));
for(int i=0;i<DIS+1;i++)
dp[0][i] = -INF;
dp[0][OFFSET] = 0;//core
}
int
main(void)
{
int t;
while(~scanf("%d", &t))
{
for(int k=0; k<t; k++)
{
bool hasZero = false;
init();
int n;
scanf("%d", &n);
int cnt = 0;
for(int i=0; i<n; ++i)
{
scanf("%d", &w[++cnt]);
if(w[cnt]==0)
{
hasZero = true;//zero weight exist
cnt--;
}
}
n = cnt;//new count
for(int i=1; i<=n; ++i)
{
for(int j=0; j <= DIS; j++)
{
dp[i][j] = MAX(dp[i-1][j],
MAX(dp[i-1][j+w[i]]+w[i],
dp[i-1][j-w[i]]+w[i])
);
}
}
printf("Case %d: ",k+1);
if(dp[n][OFFSET] == 0)
puts( hasZero==true?"0":"-1");
else
printf("%d\n",dp[n][OFFSET]/2);
}
}
return 0;
}