杭电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

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
/*
母函数解法:将能筹成的标记成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;
} 
时间: 2024-09-28 14:39:31

杭电1171 Big Event in HDU(母函数+多重背包解法)的相关文章

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

HDU 1171 Big Event in HDU(多重背包)

Big Event in HDU 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 i

HDU 1171 Big Event in HDU【多重背包】

大意:有n个物品,告诉你每个物品的价值问能否分成价值a.b两份使{a > b  && a + b == 总价值  && a - b尽量小} 分析:多重背包 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 55; 7 const int INF = 1000000

Big Event in HDU(多重背包套用模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28483    Accepted Submission(s): 10027 Problem Description Nowadays, we all know

hdu 1171 Big Event in HDU 母函数解法。。以后再来用背包A一下

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25973    Accepted Submission(s): 9139 Problem Description Nowadays, we all know that Computer College is the biggest department

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): 22697    Accepted Submission(s): 7969 Problem Description Nowadays, we all know that Computer College is the biggest department

HDU1171--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): 1139 Accepted Submission(s): 444 Problem Description Nowadays, we all know that Computer College is the biggest department in HD

hdu1171--C - Big Event in HDU(多重背包+二进制优化)

C - Big Event in HDU Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College

HDU 1171 Big Event in HDU(0-1背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:给出一系列的价值,需要平分,并且尽量接近. 思路:0—1背包问题. 0-1背包问题也就是有n种物品且每种只有一个.第i个物品的体积为vi,重量为wi.选择一些物品装到背包中,使得体积不超过背包的前提下重量尽可能大. 用f(i,j)表示“把前i个物品装到容量为j的背包中的最大总重量,其状态转移方程就是: f(i,j)=max{ f(i-1,j),f(i-1,j-v[i])+w[i] } 所以在第i个