POJ 2576 / Light OJ 1147 Tug of War 状态压缩DP
来源:互联网 发布:windows longhorn开机 编辑:程序博客网 时间:2024/06/10 21:16
Tug of War
Time Limit: 3000MS Memory Limit: 65536KTotal 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
310090200
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;}
阅读全文
0 0
- POJ 2576 / Light OJ 1147 Tug of War 状态压缩DP
- Light oj 1147 - Tug of War(状压dp)
- Light OJ 1147 Tug of War (背包+状压)
- POJ 2576 Tug of War
- POJ 2576 Tug of War
- light oj 1147 背包dp+状态压缩 好题
- lightoj 1147 - Tug of War DP压位
- light oj 1018 状态压缩DP
- light oj 1021 状态压缩dp
- lightoj 1147 - Tug of War
- POJ 2576 Tug of War(模拟退火)
- POJ 2576 Tug of War 二维费用背包
- POJ 2576 Tug of War 二维背包 OR 随机化
- ZOJ1880 POJ2576 Tug of War,DP
- lightoj 1147 - Tug of War 动态规划
- Tug of War
- Tug of War
- poj2576 tug of war
- ARP协议解析以及APR脚本
- ActiveMQ实战(二)--安装配置ActiveMQ
- kmalloc、vmalloc、malloc的区别
- bzoj 3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(SG函数)
- 三道智力题
- POJ 2576 / Light OJ 1147 Tug of War 状态压缩DP
- APK瘦身——更全面的方案
- Monto Carlo估计动作价值(action values)
- 启动minicom时出现错误"Device /dev/ttyS0 is locked minicom"的解决办法
- 前端学习(六)回调,异步,非阻塞,事件驱动等知识
- vuex学习之路之出现的错误一
- 深入理解JVM(六)——类文件结构——code
- [模板]二维凸包(纯代码)
- 树的后序遍历方式源码