鱼C论坛

 找回密码
 立即注册
查看: 2595|回复: 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 | 显示全部楼层
#include<cstdio>
#define OFFSET 2000
#define INF 0x7fffffff

using namespace std;
int list[101];
int dp[101][4001];

int main()
{
        int T,n;
        int cas=0;
        bool have0;
        scanf("%d",&T);
        while(T--)
        {
                cas++;
                scanf("%d",&n);
                have0=false;
                int cnt=0;
                for(int i=1;i<=n;i++)
                {
                        scanf("%d",&list[++cnt]);
                        if(list[cnt]==0)
                        {
                                cnt--;
                                have0=true;
                        }
                }
                n=cnt;
                for(int i=-2000;i<=2000;i++)
                        dp[0][i+OFFSET]=-INF;
                dp[0][OFFSET]=0;
                for(int i=1;i<=n;i++)
                {
                        for(int j=-2000;j<=2000;j++)
                        {
                                int a=-INF,b=-INF,c;
                                if(j-list[i]>=-2000)
                                        a=dp[i-1][j-list[i]+OFFSET]+list[i];
                                if(j+list[i]<=2000)
                                        b=dp[i-1][j+list[i]+OFFSET]+list[i];
                                c=dp[i-1][j+OFFSET];
                                a=a>b?a:b;
                                dp[i][j+OFFSET]=a>c?a:c;
                        }
                        
                }
                if(dp[n][OFFSET]==0)
                        printf("Case %d: %d\n",cas,have0==true?0:-1);
                else
                        printf("Case %d: %d\n",cas,dp[n][OFFSET]/2);
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-6 07:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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