首页 > 代码库 > HDU 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)Total Submission(s): 21671 Accepted Submission(s): 7598
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
2 10 1 20 1 3 10 1 20 2 30 1 -1
Sample Output
20 10 40 40
题意:有n样物品,每样物品价值是v,件数是m。尽量把这些物品分成两堆使得两边总价值最接近。输出分得的两堆各自的价值。
利用母函数法来解决,因为分成两堆,而两堆中较小的一堆最大为所有物品总价值量的一半,所以母函数的组合数上下就可以设置成总价值量的一半。求出所有的组合后,可以利用贪心的思想来得到答案,因为要求两堆之差尽可能小,所以就可以从总价值量的一半开始向小的方向找,找到最大的价值量,则另一堆的价值量就是总价值量-此堆的价值量。因为组合数可能较大,这里不记录组合种数,而是用一个标记来表示该数能否组合出即可。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; const int MAX = 51; bool ans[125002],tmp[125002]; int p[MAX],m[MAX],n; void maxx(int &a,int &b){ int tmp; if(a<b){ tmp = a; a = b; b = tmp; } } void work(int limit){ int i,j,k; memset(ans,0,sizeof(ans)); memset(tmp,0,sizeof(tmp)); for(i=0;i<=m[0];++i){ ans[i*p[0]] = true; } for(i=1;i<n;++i){ for(j=0;j<=limit;++j){ for(k=0;k<=m[i] && j+k*p[i]<=limit;++k){ if(tmp[j+k*p[i]] || ans[j]){ tmp[j+k*p[i]] = true; } } } for(j=0;j<=limit;++j){ ans[j] = tmp[j]; tmp[j] = false; } } } int main(){ //freopen("in.txt","r",stdin); //(author : CSDN iaccepted) int all,vl,vr,i; while(scanf("%d",&n)!=EOF){ if(n<0)break; all = 0; for(i=0;i<n;++i){ scanf("%d %d",&p[i],&m[i]); all += p[i] * m[i]; } work((all + 1)/2); for(i=(all + 1)/2;i>=0;--i){ if(ans[i]){ vl = i; break; } } vr = all - vl; maxx(vl,vr); printf("%d %d\n",vl,vr); } return 0; }
HDU 1171 Big Event in HDU (母函数)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。