您的位置:首页 > 其它

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

2017-07-22 13:13 330 查看
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

Waterloo local 2000.09.30

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

写了个如下的一维背包:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: