HDU1171-Big Event In HDU(母函数、动态规划)
来源:互联网 发布:ios电影下载软件 编辑:程序博客网 时间:2024/05/16 17:42
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16416 Accepted Submission(s): 5792
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
题目大意:给定一些物品,每种物品都有固定价值和数量,现在要把这些物品尽可能拼接分成两份(单个物品不能切割),请输出分割后各份的值(如果不一般大,则先输出大的)。
分析:
思路一:我们可以利用母函数,建立该模型的母函数,算出x^(sum/2)的系数即可!代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>int c1[125100],c2[125100],f[55],v[55];int main() { int n,i,j,k,aim,sum; while(scanf("%d",&n),n>=0) { for(i=1,sum=0;i<=n;i++) { scanf("%d %d",&v[i],&f[i]); sum+=f[i]*v[i]; } aim=sum/2; memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); for(j=0,k=0;k<=f[1];k++,j+=v[1])//第一个括号里面只能取到这么多 c1[j]=1; for(i=2;i<=n;i++) { for(j=0;j<=aim;j++) { if(c1[j]==0) continue; for(k=0;k*v[i]+j<=aim&&k<=f[i];k++) c2[k*v[i]+j]=c1[j]; } for(j=0;j<=aim;j++) { c1[j]=c2[j]; c2[j]=0; } } for(i=aim;i>=0;i--) if(c1[i]!=0) break; printf("%d %d\n",sum-i,i); } return 0;}
思路二:利用动态规划之多重背包问题,我们可以看成是背包问题,背包容量是总价值的一般,目标是挑出一些物品,尽可能装满背包。
方法一:转化成0-1背包来做。代码:#include<stdio.h>#include<string.h>#include<stdlib.h>int dp[126000],v[60],w[110];int main(){ int n,i,j,k,sum; while(scanf("%d",&n),n>=0) { for(i=0,sum=0;i<n;i++) { scanf("%d %d",&v[i],&w[i]); sum+=v[i]*w[i]; } memset(dp,0,sizeof(dp)); for(i=0;i<n;i++) for(j=1;j<=w[i];j++) for(k=sum/2;k>=v[i];k-=v[i]) if(dp[k-v[i]]+v[i]>dp[k]) dp[k]=dp[k-v[i]]+v[i]; printf("%d %d\n",sum-dp[sum/2],dp[sum/2]); } return 0;}
方法二:将每种物品打包成价值为1、2、4、。。等,加快挑选过程!代码:#include<stdio.h>#include<string.h>#define N 55#define M 1250010int dp[M],f[N+1],v[N+1];int main(){ int i,j,k,flag,sum,s,n; while(scanf("%d",&n),n>=0) { for(i=1,sum=0;i<=n;i++){ scanf("%d %d",&v[i],&f[i]); sum+=f[i]*v[i]; } memset(dp,0,sizeof(dp[0])*(sum/2+10)); for(i=1;i<=n;i++){ for(j=sum/2;j>=v[i];j--) { for(k=1,s=0;k<=f[i]&&j>=k*v[i];k*=2) { dp[j]=dp[j-k*v[i]]+k*v[i]>dp[j]?dp[j-k*v[i]]+k*v[i]:dp[j]; } k=f[i]-s; if(j>=k*v[i]) dp[j]=dp[j-k*v[i]]+k*v[i]>dp[j]?dp[j-k*v[i]]+k*v[i]:dp[j]; } } printf("%d %d\n",sum-dp[sum/2],dp[sum/2]); } return 0;}
- HDU1171-Big Event In HDU(母函数、动态规划)
- hdu1171 Big Event in HDU 动态规划
- HDU1171 Big Event in HDU 【母函数】
- 【HDU1171】Big Event in HDU 母函数
- HDU1171 Big Event in HDU (母函数)
- HDU1171 Big Event in HDU,母函数,终极模板
- hdu1171 Big Event in HDU(生成函数)
- hdu1171 Big Event in HDU(DP)
- HDU1171 Big Event in HDU
- HDU1171--Big Event in HDU
- HDU1171 Big Event in HDU
- HDU1171 Big Event in HDU
- hdu1171 Big Event in HDU
- hdu1171 Big Event in HDU
- HDU1171-Big Event in HDU
- hdu1171 Big Event in HDU
- HDU1171 Big Event in HDU
- hdu1171 Big Event in HDU
- Struts2工程下用jsp接收文件上传问题
- UBUNTU下连接ANDROID手机调试
- Unix编辑器概述
- nbut[1408] Dolly Song 大水题 辅音+元音 输出此类单词的个数
- Java JDK5.0的新特性 增强for循环 自动装箱/拆箱 可变参数 静态导入
- HDU1171-Big Event In HDU(母函数、动态规划)
- UVA 10878
- ASP.NET request.form获取不到值的问题
- android 源码域名变更 kernel download (下载)方法
- A Baseline for Front-End Developers
- WCF 配置说明
- AE+C#实现缩放坐标比例尺长度面积量测输出图片等功能
- 重定向 Bash “time” 命令的输出
- C#流总结