hdu1171 完全背包 两种解法 多重背包
来源:互联网 发布:学雅思的软件 编辑:程序博客网 时间:2024/05/29 15:38
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21202 Accepted Submission(s): 7460
Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
很明显完全背包 容量为总的一半
数组要开大不然运行错误
#include<iostream>#include<cstring>#include<algorithm>using namespace std;int dp[500005],w[1005],num[1005];int main(){int n,m;while(cin>>n&&n>=0){memset(dp,0,sizeof(dp));int i,j,v_sum=0,k;for(i=1;i<=n;++i){cin>>w[i]>>num[i];v_sum+=w[i]*num[i];}m=v_sum/2;for(i=1;i<=n;++i){for(k=1;k<=num[i];++k){for(j=m;j>=w[i];--j){dp[j]=max(dp[j-w[i]]+w[i],dp[j]); }}}cout<<v_sum-dp[m]<<" "<<dp[m]<<endl;}}
#include<iostream>#include<cstring>#include<algorithm>using namespace std;int dp[500005],w[1005];int main(){int n,m;while(cin>>n&&n>=0){memset(dp,0,sizeof(dp));int i,j,a,v_sum=0;for(i=1;i<=n;++i){cin>>w[i]>>a;v_sum+=w[i]*a;}m=v_sum/2;for(i=1;i<=n;++i){for(j=w[i];j<=m;++j){dp[j]=max(dp[j-w[i]]+w[i],dp[j]); }}cout<<v_sum-dp[m]<<" "<<dp[m]<<endl;}}
多重背包解法
#include<iostream>#include<cstring>#include<algorithm>using namespace std;#define N 350000int dp[N],w[1000],v[1000];int main(){int n;while(cin>>n&&n>=0){int i,j,k;memset(dp,0,sizeof(dp));int weight,value,number;int count=1,sum=0;;for(i=1;i<=n;++i) {cin>>weight>>number;value=weight;sum+=weight*number;for(k=1;k<=number;k<<=1){v[count]=k*value;w[count++]=k*weight;number-=k;}if(number>0){v[count]=number*value;w[count++]=number*weight;}}for(i=1;i<=count-1;++i)for(j=sum/2;j>=w[i];--j)dp[j]=max(dp[j],dp[j-w[i]]+v[i]); cout<<sum-dp[sum/2]<<" "<<dp[sum/2]<<endl;}return 0;}
104876352014-04-06 20:18:04Accepted11710MS2252K643 BC++的鱼104876062014-04-06 20:13:55Accepted117146MS1668K840 BC++向往蓝天的鱼
0 0
- hdu1171 完全背包 两种解法 多重背包
- hdu1171多重背包
- hdu1171多重背包
- hdu1171(多重背包)
- hdu1171(多重背包)
- hdu1171多重背包
- hdu1171 多重背包
- hdu1171(多重背包)
- hdu1171 01背包 | 多重背包
- hdu1171(多重背包模板题)
- POJ 1742 Coins (多重背包的两种解法)
- 三种背包模版,01背包,完全背包,多重背包
- hdu2844 多重背包+二进制优化(多重背包的完全背包优化解法)
- 多重背包经典列题hdu1171
- Big Event in HDU hdu1171 多重背包
- HDU1171:Big Event in HDU(多重背包)
- hdu1171 Big Event in HDU (多重背包)
- hdu1171---Big Event in HDU(多重背包)
- CI环境设置脚本
- Eclipse调试Bug的七种常用技巧
- C数据类型,变量,运算符,数组与字符串
- unity3D游戏开发实战原创视频讲座系列6之生死逃亡游戏开发
- 一定有文章
- hdu1171 完全背包 两种解法 多重背包
- WDF驱动学习笔记一 - 熊猫正正的日志
- 一致性哈希算法 理解及实际应用中 优化
- 数据库知识总结之ER图的设计
- Eclipse androidADT Unhandled event loop exception No more handles
- 【js学习笔记-115】----html5之地理位置
- 第六周 项目1 体验常成员函数
- 关于 Private strand flush not complete
- vbs 异常
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
广州住房公积金管理中心
西安住房公积金
成都住房公积金管理中心
宜春住房公积金
安徽省住房和城乡建设厅
四川城乡住房建设厅
陕西省住房公积金中心
深圳市住房和建设局
南宁市住房保障和房产管理局
住房公积金是什么
十堰住房公积金查询
广安住房公积金查询
枣庄住房公积金管理中心
南宁市住房公积金查询
陕西住房公积金查询网
南充市住房公积金管理中心
南宁住房公积金
晋中住房公积金查询个人账户
保定住房公积金查询
淮南市住房公积金查询
苏州市住房公积金管理中心
宜春市住房公积金管理中心
住房公积金管理中心电话
福州住房公积金查询
玉林住房公积金查询
住房公积金管理中心地址
烟台住房公积金查询个人账户
个人住房公积金余额查询
成都市住房公积金中心
东莞住房公积金个人帐户查询
西安市住房公积金查询
黄石住房公积金查询
住房公积金客服电话
保定住房公积金
郑州住房公积金
住房公积金咨询电话
泉州市住房公积金个人查询
南充住房公积金查询个人账户
住房公积金电话号码
银川住房公积金查询
淮南住房公积金查询个人账户