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
 

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 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 
#include 
#include 
#include 

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=0;--i){
			if(ans[i]){
				vl = i;
				break;
			}
		}
		vr = all - vl;
		maxx(vl,vr);
		printf("%d %d\n",vl,vr);
	}
    return 0;
}



你可能感兴趣的:(ACM-母函数,ACM汇总,HDU,1171,母函数)