-
题目描述:
-
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
-
-
#include <stdio.h> #include <algorithm> using namespace std; int buf[101]; int dp[101][4001]; //状态,表示前i个橘子被选中后,第一堆比第二堆重j时的两堆最大重量和 const int OFFSET = 2000; //偏移值,-2000到2000 const int INF = 0x7fffffff; int main() { //freopen("1453in.txt","r",stdin); int T; int cas = 1; scanf("%d",&T); while(T--) { int n, i, j; scanf("%d",&n); bool Zero = false; for(i = 1; i <= n; i++) { scanf("%d",&buf[i]); if(buf[i] == 0) Zero = true; // 有重量为0的橘子。。。。。 } sort(buf,buf+n); for(i = -2000; i <= 2000; i++) dp[0][i+OFFSET] = -INF; dp[0][0+OFFSET] = 0; for(i = 1; i <= n; i++) { for(j = -2000; j <= 2000; j++) { int tmp1 = -INF, tmp2 = -INF; if(buf[i]+j <= 2000 && dp[i-1][buf[i]+j+OFFSET] != -INF) tmp1 = dp[i-1][buf[i]+j+OFFSET] + buf[i]; //放入第一堆 if(j-buf[i] >= -2000 && dp[i-1][j-buf[i]+OFFSET] != -INF) tmp2 = dp[i-1][j-buf[i]+OFFSET] + buf[i]; //放入第二堆 if(tmp1 < tmp2) tmp1 = tmp2; if(tmp1 < dp[i-1][j+OFFSET]) //不放入任何堆 tmp1 = dp[i-1][j+OFFSET]; dp[i][j+OFFSET] = tmp1; //存入三者中最大值 } } printf("Case %d: ",cas); if(dp[n][0+OFFSET] == 0) { if(Zero == true) printf("0\n"); else printf("-1\n"); } else printf("%d\n",dp[n][0+OFFSET]/2); cas++; } return 0; }
Tino面临一个使用‘搬运杆’平衡携带橙子的问题,目标是两边重量相等。每个橙子都有自己的重量,他想知道能携带的最大总重量是多少。这是一个运用贪婪算法来求解的问题。
2702

被折叠的 条评论
为什么被折叠?



