Tug of War(嚴格限制數量的二維費用揹包)

Tug of War
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8500   Accepted: 2311

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

Source

Waterloo local 2000.09.30


#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int dp[45010][105],w[105];
int main()
{
    int i,j,k,n,sum,sum1,sum2,num;
    while(cin>>n)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            cin>>w[i];
            sum+=w[i];
        }
        sum1=(sum+1)/2;
        num=(n+1)/2;
        memset(dp,0,sizeof(dp));
        //cout<<"ok"<<endl;
        for(i=1;i<=n;i++)
        {
            for(j=sum1;j>=w[i];j--)
            {
                for(k=num;k>=1;k--)
                {
                    dp[j][k]=max(dp[j][k],dp[j-w[i]][k-1]+w[i]);
                }
            }
        }
        if(dp[sum1][num]>sum-dp[sum1][num])
        {
            cout<<sum-dp[sum1][num]<<" "<<dp[sum1][num]<<endl;
        }
        else
        {
            cout<<dp[sum1][num]<<" "<<sum-dp[sum1][num]<<endl;
        }
    }
}

不知道怎麼回事?用二維費用揹包竟然過了,應該過不了!


5
50 2 25 21 2
正確答案應該是48 52
正確的應該是必須在n/2人之內找到重量不大於總重量一半的重量!

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int dp[45010][105],w[105];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
    int i,j,k,n,sum,sum1,sum2,num,ans;
    while(cin>>n)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            cin>>w[i];
            sum+=w[i];
        }
        sum1=(sum+1)/2;
        num=(n+1)/2;
        memset(dp,-1,sizeof(dp));
		dp[0][0]=0;
        //cout<<"ok"<<endl;
        for(i=1;i<=n;i++)
        {
            for(j=sum1;j>=w[i];j--)
            {
                for(k=num;k>=1;k--)
                {
					if(dp[j-w[i]][k-1]!=-1)
						dp[j][k]=max(dp[j][k],dp[j-w[i]][k-1]+w[i]);
                }
            }
        }
		ans=0;
		for(i=0;i<=sum1;i++)
		{
			if(dp[i][num]>ans)
			{
				ans=dp[i][num];
			}
			if(dp[i][num-1]>ans)
			{
				ans=dp[i][num-1];
			}
		}
		if(ans<sum-ans)
        {
            cout<<ans<<" "<<sum-ans<<endl;
        }
        else
        {
            cout<<sum-ans<<" "<<ans<<endl;
        }
    }
}


發佈了4 篇原創文章 · 獲贊 22 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章