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;	
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

【hdoj1171】Big Event in HDU(01背包)

Big Event in HDU 大略题意:   Hdu的计算机系曾分为计算机学院和软件学院,故原来学院的公共设施要尽可能均分给这两个新的学院,要求计算机学院分得的设施不能少于软件学院的 输...

杭电1171Big Event in HDU

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • z8110
  • z8110
  • 2015-09-18 18:39
  • 203

HDoj-1171-Big Event in HDU-母函数

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

母函数之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。      ...

HDOJ 1171 Big Event in HDU解题报告

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

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

题意很简单,n种物品,每个物品对应一个价值和数量。让你竟可能的分成相等的两份。 可以设背包容量为sum/2用多重背包解决。 也可以用母函数来解决,计算能组成的最接近sum/2的值。 母函数...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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