首页 > 其他 > 详细

HDU-2576 Tug of War

时间:2014-03-10 13:26:38      阅读:475      评论:0      收藏:0      [点我收藏+]

 

http://poj.org/problem?id=2576

二维数组01背包的变形。

Tug of War
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8147   Accepted: 2191

Description

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

Sample Input

3
100
90
200

Sample Output

190 200
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int weight[120],sum,sum1,dp[50000][103];
int Max(int x,int y)
{
    if(x>y)
    return x;
    else
    return y;
}
int main()
{
    
    int n,i,n1,j,k;
      while(~scanf("%d",&n))
      {
       memset(dp,0,sizeof(dp));
        sum=0;
      for(i=1;i<=n;i++)
        {
         cin>>weight[i];
         sum+=weight[i];
        }
        dp[0][0]=1;
       sum1=sum/2;
       n1=(n+1)/2;
      for(i=1;i<=n;i++)
       for(j=sum;j>=weight[i];j--)
         for(k=n1;k>0;k--)
             if(dp[j-weight[i]][k-1])
                  dp[j][k]=1;
        for(i=sum1;i>=0;i--)
         if(dp[i][n1]||dp[i][n1-1])
              break;
        cout<<i<<" "<<sum-i<<endl;
 
      }
      return 0;
}

HDU-2576 Tug of War,布布扣,bubuko.com

HDU-2576 Tug of War

原文:http://www.cnblogs.com/cancangood/p/3590744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!