Tug of War(严格限制数量的二维费用背包)
2015-07-26 15:54
597 查看
Tug of War
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
Sample Output
Source
Waterloo local 2000.09.30
不知道怎么回事?用二维费用背包竟然过了,应该过不了!
正确的应该是必须在n/2人之内找到重量不大于总重量一半的重量!
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8500 | Accepted: 2311 |
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; } } }
相关文章推荐
- VS2015开发安卓应用笔记(一)
- 2.1 Qemu用户态 Machine与cpu管理
- matlab图像处理 笔记
- 第一条:了解Objective-C语言的起源
- error C2660: “Gdiplus::GdiplusBase::operator new”: 函数不接受 3 个参数(转)
- Machine Learning 1---Introduction
- Hbase总结(四)- Hbase与传统数据库的区别
- uva10561(博弈)
- 二维费用背包
- JSP之forward动作
- TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
- Oracle12c功能增强新特性之维护&升级&恢复&数据泵等
- 新的Ubuntu系统中安装系统必备工具之中文输入法、tftp、nfs、VIM、ckermit
- Cube Simulation zoj3429 模拟
- HTML5基础19----XHTML的使用规范
- android-async-http框架
- [注]省时利器:代码美化与格式化工具
- hibernate和sleep的区别
- 推荐每个PROFIBUS网段内至多连接32个从设备
- HDU 1042 N!【大数】