关闭

hdu-1171-Big Event in HDU

标签: 背包
665人阅读 评论(0) 收藏 举报
分类:

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





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44884次
    • 积分:1058
    • 等级:
    • 排名:千里之外
    • 原创:53篇
    • 转载:17篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论