hdu-1171-Big Event in HDU

原创 2013年12月03日 11:42:40

Big Event in HDU

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


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


如果用cin的话2000+毫秒,用scanf的话1200+毫秒。

#include <cstdio>
#include <string.h>
#include <iostream>
using namespace std;
int n,ans[250005],c[55],w[55],sum;
int main()
{
    while(scanf("%d",&n)&&n>=0){
        memset(ans,0,sizeof(ans));
        sum=0;
        for(int i=1; i<=n; i++){
            scanf("%d%d",&c[i],&w[i]);
            sum += c[i]*w[i];
        }
        for(int i=1; i<=n; i++){
            for(int j=1; j<=w[i]; j++){
                for(int k=sum; k>=c[i]; k--){
                    ans[k]=max(ans[k], ans[k-c[i]]+c[i]);
                }
            }
        }
        for(int i=(sum%2==0)?sum/2:sum/2+1; i<=sum; i++){
            if(i==ans[i]){
                    printf("%d %d\n",ans[i],sum-ans[i]);
                    break;
                }
        }
    }
    return 0;
}


附上640毫秒的

#include <cstdio>
#include <string.h>
using namespace std;
int n,tot; 
int v[55],m[55];
bool d[250005]; 
int main(){
	while(scanf("%d",&n)&&n>=0){
		int tot=0;
		for(int i=1;i<=n;i++){
			scanf("%d%d",&v[i],&m[i]); 
			tot+=v[i]*m[i];
		}
		
		//多重背包 每种物品数目固定 DP 
		memset(d,0,sizeof d);
		d[0]=true;
		for(int i=1;i<=n;i++){
			for(int k=1;k<=m[i];k++){
				for(int j=tot;j>=v[i];j--){
					if(d[j-v[i]])d[j]=true;		
				}	
			}	
		}	
		//输出答案 
		int ind=0;
		for(int i=tot/2,j=tot/2;;i++,j--){
			if(d[i]){ind=i;break;}
			if(d[j]){ind=j;break;}	
		}
		if(ind<tot-ind)ind=tot-ind;
		printf("%d %d\n",ind,tot-ind);
	} 
	return 0;	
}





母函数之Big Event in HDU hdoj 1171

/* Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O...

HDOJ--1171--Big Event in HDU

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

HDOJ-1171-Big Event in HDU 解题报告

普通母函数。题意:有最多50种不同价值的机器,每种机器的价值不超过50并且数量不超过100,现在要把这些机器分成A和B两部分,使两部分机器的价值尽可能相等且A的价值不能够小于B。      ...
  • JZQT_T
  • JZQT_T
  • 2015年05月21日 18:56
  • 381

HDOJ 1171 Big Event in HDU解题报告

//错误的解答 //#include #include using namespace std; bool money[250002]; unsigned value[52]; unsigne...

Big Event in HDU 【HDOJ-1171-母函数】

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...

HDOJ1171 Big Event in HDU

Big Event in HDU Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other...

hdoj 1171 Big Event in HDU(背包dp+母函数)

【题目大意】:给出n种数,每种数有m个,问怎么把这n*m个数分成两部分,满足第一部分大于等于第二部分,且尽可能相等。 【解题思路】:dp,多重背包问题。                  ...
  • new_wu
  • new_wu
  • 2012年03月30日 02:21
  • 450

hdoj 1171 Big Event in HDU(母函数/多重背包)

题意很简单,n种物品,每个物品对应一个价值和数量。让你竟可能的分成相等的两份。 可以设背包容量为sum/2用多重背包解决。 也可以用母函数来解决,计算能组成的最接近sum/2的值。 母函数...
  • CillyB
  • CillyB
  • 2016年10月26日 21:13
  • 224

hdoj 1171 Big Event in HDU (母函数)(背包)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...

杭电 1171 Big Event in HDU

Big Event in HDU                                                   Time Limit: 10000/5000 MS (Java/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu-1171-Big Event in HDU
举报原因:
原因补充:

(最多只允许输入30个字)