杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23728 Accepted Submission(s): 8363
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).
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 is not less than B.
2
10 1
20 1
3
10 1
20 2
30 1
-1
20 10
40 40/*
母函数解法:将能筹成的标记成1,找最接近sum/2的就能够
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::max;
const int MAX=52;
bool c1[MAX*MAX*100];//c2[MAX*MAX*100];
int main(){
int N;
int val[MAX],Count[MAX];
while(scanf("%d",&N),N>0){
int sum=0;
memset(val,0,sizeof(val));
memset(Count,0,sizeof(Count));
memset(c1,0,sizeof(c1));
for(int i=1;i<=N;i++){
scanf("%d%d",&val[i],&Count[i]);
sum+=val[i]*Count[i];
}
c1[0]=1;
int Limit=sum>>1;
for(int i=1;i<=N;i++){
for(int j=0;j<=Limit;j++){
for(int k=0;j+k<=Limit&&k<=val[i]*Count[i];k+=val[i])
c1[k+j]=(c1[k+j]==1?c1[k+j]:c1[j]);//注意此处不能直接等于,防止出现 50 30 20 ,v[20]=0;的情况,是能够筹成50的
}
/*
for(int j=0;j<=Limit;j++){
c1=c2[j];
c2[j]=0;
}*/
}
int k=Limit;
while(1){
if(c1[k]){
printf("%d %d\n",sum-k,k);
break;
}
k--;
}
}
return 0;
}/*
多重背包解法:
将体积和价值都看做 价值,上限也是 sum/2,找最大值,用sum-d[sum/2]便是较大的那一个
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using std::max;
const int MAX=51;
int sum,Count,N;
int dp[MAX*MAX*100];
int v[MAX],w[MAX],c[MAX];
int V[MAX],W[MAX];
void Div(){//多重背包分解
Count=0;
for(int i=0;i<N;i++){
for(int j=1;j<=c[i];j<<=1){
W[Count++]=w[i]*j;
c[i]-=j;
}
if(c[i]>0){
W[Count++]=w[i]*c[i];
}
}
}
int main(){
while(scanf("%d",&N),N>0){
sum=0;
memset(w,0,sizeof(w));
memset(c,0,sizeof(c));
memset(W,0,sizeof(W));
memset(dp,0,sizeof(dp));
for(int i=0;i<N;i++){
scanf("%d%d",&w[i],&c[i]);
sum+=w[i]*c[i];
}
int upLimit=sum>>1;
Div();
for(int i=0;i<Count;i++){
for(int j=upLimit;j>=W[i];j--){
dp[j]=max(dp[j],dp[j-W[i]]+W[i]);
}
}
printf("%d %d\n",sum-dp[upLimit],dp[upLimit]);
}
return 0;
}
杭电1171 Big Event in HDU(母函数+多重背包解法)的更多相关文章
- HDU 1171 Big Event in HDU (多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- HDU 1171 Big Event in HDU(多重背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- hdu 1171 Big Event in HDU(多重背包+二进制优化)
题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...
- Big Event in HDU(多重背包套用模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...
- HDU 1171 Big Event in HDU 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory ...
- HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU1171--Big Event in HDU(多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1171 Big Event in HDU(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:给出一系列的价值,需要平分,并且尽量接近. 思路:0—1背包问题. 0-1背包问题也就是有n种物品且 ...
随机推荐
- 高效合并两个有序数组(Merge Sorted Array)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- Android---两个视图间的淡入淡出
本文译自:http://developer.android.com/training/animation/crossfade.html 淡入淡出动画(也可以作为溶解动画)是指在渐渐的淡出一个UI组件的 ...
- 公布一个软件,轻新视频录播程序,H264/AAC录制视音频,保存FLV,支持RTMP直播
已经上传到CSDN,下载地址:http://download.csdn.net/detail/avsuper/7421647,不要钱滴,嘿嘿... 本程序能够把摄像头视频和麦克风音频,录制为FLV文件 ...
- 【剑指offer】约瑟夫环问题
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27957407 题目描写叙述: 每年六一儿童节,JOBDU都会准备一些小礼物去看望孤儿院的小 ...
- [Java聊天室server]实战之五 读写循环(服务端)
前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...
- exit()和_exit()和return
exit()和return的差别: 依照ANSI C,在最初调用的main()中使用return和exit()的效果同样. 但要注意这里所说的是"最初调用".假设main()在一个 ...
- java 中间 final修饰符
修饰符final:它是一个常数,我不同意改变 ,可以修改 变数,办法 ,分类 final修改变量:是final成常量,一旦赋值不能改变 常量能够在初始化时直接赋值.也能够在构造方法里赋值.仅仅能在这两 ...
- HTML转义字符大全(转)
1.常用转义字符 转义字符串(Escape Sequence)也称字符实体(Character Entity).在HTML中,定义转义字符串的原因有两个:第一个原因是像“<”和“>”这类符 ...
- Berkeley DB 使用
http://www.ibm.com/developerworks/cn/linux/l-embdb/index.html UNIX/LINUX平台下的数据库种类非常多,参考资料1中 列举了其中的大部 ...
- ArcGIS For Flex给定两个
1.错误叙述性说明 2.错误原因 3.解决方案 版权声明:本文博客原创文章,博客,未经同意,不得转载.