鱼C论坛

 找回密码
 立即注册
查看: 2378|回复: 1

[技术交流] 040:Greedy Tino

[复制链接]
发表于 2018-2-17 21:09:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Problem Description

      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.

Input

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.



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
1
5
1 2 3 4 5



Sample Output
Case 1: 7

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-17 21:37:51 | 显示全部楼层
  1. #include<cstdio>
  2. #define OFFSET 2000
  3. #define INF 0x7fffffff

  4. using namespace std;
  5. int list[101];
  6. int dp[101][4001];

  7. int main()
  8. {
  9.         int T,n;
  10.         int cas=0;
  11.         bool have0;
  12.         scanf("%d",&T);
  13.         while(T--)
  14.         {
  15.                 cas++;
  16.                 scanf("%d",&n);
  17.                 have0=false;
  18.                 int cnt=0;
  19.                 for(int i=1;i<=n;i++)
  20.                 {
  21.                         scanf("%d",&list[++cnt]);
  22.                         if(list[cnt]==0)
  23.                         {
  24.                                 cnt--;
  25.                                 have0=true;
  26.                         }
  27.                 }
  28.                 n=cnt;
  29.                 for(int i=-2000;i<=2000;i++)
  30.                         dp[0][i+OFFSET]=-INF;
  31.                 dp[0][OFFSET]=0;
  32.                 for(int i=1;i<=n;i++)
  33.                 {
  34.                         for(int j=-2000;j<=2000;j++)
  35.                         {
  36.                                 int a=-INF,b=-INF,c;
  37.                                 if(j-list[i]>=-2000)
  38.                                         a=dp[i-1][j-list[i]+OFFSET]+list[i];
  39.                                 if(j+list[i]<=2000)
  40.                                         b=dp[i-1][j+list[i]+OFFSET]+list[i];
  41.                                 c=dp[i-1][j+OFFSET];
  42.                                 a=a>b?a:b;
  43.                                 dp[i][j+OFFSET]=a>c?a:c;
  44.                         }
  45.                        
  46.                 }
  47.                 if(dp[n][OFFSET]==0)
  48.                         printf("Case %d: %d\n",cas,have0==true?0:-1);
  49.                 else
  50.                         printf("Case %d: %d\n",cas,dp[n][OFFSET]/2);
  51.         }
  52.         return 0;
  53. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 04:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表