POJ 2576 / Light OJ 1147 Tug of War 状态压缩DP

Tug of War
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9257 Accepted: 2578

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



看到题的第一反应,一维背包。

写了个如下的一维背包:

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f; 
const ld pi=3.1415926535898L;
int a[105],dp[maxn];

int main() {
	int cas;
//	scanf("%d",&cas);
//	while (cas--) {
		int n,i,j,tot=0,ans;
		scanf("%d",&n);
		for (i=1;i<=n;i++) {
			scanf("%d",&a[i]);
			tot+=a[i];
		}
		mem0(dp);
		ans=a[1];
		for (i=1;i<=n;i++) {
			for (j=tot/2;j>=a[i];j--) {
				if (dp[j-a[i]]<=(n-1)/2&&dp[j-a[i]]>0) {
					dp[j]=max(dp[j],dp[j-a[i]]+1);
					if (dp[j]>=n/2) ans=max(ans,j);
				}
			}
			dp[a[i]]=1;
		}
		printf("%d %d\n",ans,tot-ans);
//	}
	return 0;
}


POJ上过了,然而

这样是错的!!!!!!!!!

原因是,当价值一定、人数取最多时,不一定最优。可能在这个价值上,取更少的人,而后续DP过程再取一些人使总价值更加接近总价值一半。而原程序中,一旦取到总人数一半就自动不再往上取。

POJ数据真水啊。。。


正解为状态压缩。

dp[i]的第j+1位为1,表示价值 i 可以由 j 个人的价值相加得到。

然后套个背包,就好了。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f; 
const ld pi=3.1415926535898L;
ll a[105],dp[maxn];

int main() {
	int cas,cnt=0;
	scanf("%d",&cas);
	while (cas--) {
		int n,i,j;
		ll tot=0,ans,m;
		cnt++;
		scanf("%d",&n);
		m=inf;
		for (i=1;i<=n;i++) {
			scanf("%lld",&a[i]);
			tot+=a[i];
			m=min(m,a[i]);
		}
		ans=0;
		mem0(dp);
		dp[0]=1;
		for (i=1;i<=n;i++) {
			for (j=tot/2;j>=a[i];j--) {
				if (j-a[i]>=0)
				if (dp[j-a[i]]!=0)
			    	dp[j]=dp[j]|(dp[j-a[i]]<<1);
			}
		}
		for (i=tot/2;i>=0;i--) {
			if ((dp[i]>>(n/2))%2) {
				ans=i;
				break;
			}
			if (n%2) 
			    if ((dp[i]>>(n/2+1))%2) {
			    	ans=i;
				    break;
			    }
		}
		printf("Case %d: %lld %lld\n",cnt,ans,tot-ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值