HDU 1171 Big Event in HDU

题目:HDU-1085

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085

题目:

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31958    Accepted Submission(s): 11183


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).
 

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.
 

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,怎么样分成价值最接近的两堆,每一堆价值多少。

额,这个题我还是用了母函数做的,这个构造的多项式以价值为指数,和其他的一样,只不过在得出结果以后我先筛选出非0的结果,排序以后再一个个比较哪个最接近,数据比较小所以也还好,没超时。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<cstdio>
using namespace std;
const int maxn= 250005;
int a[maxn],b[maxn];
int n;
int v,m,t;
int counts;
int maxNum,ans1,ans2;
int main(){
	while(cin>>n){
		if(n<0) break;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		cin>>v>>m;
		for(int i=0;i<=m;i++)
			a[i*v]=1;
		t=v*m;
		for(int i=1;i<n;i++){
			cin>>v>>m;
			t+=v*m;
			for(int j=0;j<=t;j++)
				for(int k=0;k<=v*m;k+=v)
					b[j+k]+=a[j];
			for(int j=0;j<=t;j++){
				a[j]=b[j];
				b[j]=0;
			}
		} //以上为构建母函数得出结果
		counts=0;
		for(int i=1;i<=t;i++){                        //筛选非0解
			if(a[i]!=0)
				b[counts++]=i;
		}
		sort(b,b+counts);                           //排序,其实不排也没什么
		maxNum=maxn;
		for(int i=0;i<counts;i++){                  //记住保证大的一堆在前面
			if(t-b[i]>=b[i]){
				if(t-b[i]-b[i]<maxNum){
					maxNum=t-b[i]-b[i];
					ans1=t-b[i];
					ans2=b[i];
				}
			}
			else{
				if(2*b[i]-t<maxNum){
					maxNum=2*b[i]-t;
					ans1=b[i];
					ans2=t-b[i];
				}
			}
		}
		cout<<ans1<<" "<<ans2<<endl;
	}
	return 0;
}

good good study, day day up!

你可能感兴趣的:(HDU,1171)