TOJ-1356 Tug of War

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.

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.

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

Output for Sample Input

190 200



Source: Waterloo Local Contest Sep. 30, 2000

这道题我们看作一个背包的容量是总质量的一半,并且要放总数量的一半的背包问题。

 

dp[j][k]=1状态表示背包放入k个物品时总质量为j。

#include <stdio.h>
#include <iostream>
#include <memory.h> 
using namespace std;
int p[110],dp[45010][110];
int main(){
    int i,j,k,n,sum,m;
    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        sum = 0;
        for(i=1;i<=n;i++){
            scanf("%d",&p[i]);
            sum += p[i];
        }
        m = (n+1)/2;
        dp[0][0] = 1;
        for(i=1;i<=n;i++){
            for(j=sum/2;j>=p[i];j--){
                for(k=m;k>0;k--){
                    if(dp[j-p[i]][k-1])
                        dp[j][k] = 1;
                }
            }
        }
        for(i=sum/2;i>0;i--){
            if(n%2==0)  
            {  
                if(dp[i][m]==1)  
                    break;  
            }  
            else  
            {  
                if(dp[i][m]==1||dp[i][m-1]==1)  
                    break;  
            }
        }
        printf("%d %d\n",i,sum-i); 
    }
    return 0;
}

 

然而这道题的数据还是有漏洞的。由于限定两个背包中数量相差不超过1,所以将上述状态降为成如下状态是不行的:

dp[j]=k表示当背包内放k个物品时质量为j,该状态代码如下

#include <stdio.h>
#include <iostream>
#include <memory.h> 
using namespace std;
int p[110],dp[45010];
int main(){
    int i,j,k,n,sum,m;
    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        sum = 0;
        for(i=1;i<=n;i++){
            scanf("%d",&p[i]);
            sum += p[i];
        }
        m = (n+1)/2;
        dp[0] = 1;
        for(i=1;i<=n;i++){
            for(j=sum/2;j>=0;j--){
                if(dp[j] && dp[j]<=m && j+p[i]<=sum/2){  
                    if(!dp[j+p[i]])  
                        dp[j+p[i]] = dp[j]+1;  
                    else  
                        dp[j+p[i]] = min(dp[j+p[i]],dp[j]+1);  
                }
            }
        }
        for(i = sum/2;i>=0;i--)  
            if(dp[i]){  
            break;  
        } 
        printf("%d %d\n",i,sum-i); 
    }
    return 0;
}

显然这个代码无法限制两背包数量之差。如4个物品质量为100,20,30,50时,输出应为80,120而不是100,100。

 

转载于:https://www.cnblogs.com/shenchuguimo/p/6394636.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值