杭电 1171

来源:互联网 发布:淘宝秋季休闲鞋女鞋 编辑:程序博客网 时间:2024/05/20 01:36

Big Event in HDU

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


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
210 120 1310 1 20 230 1-1
 

Sample Output
20 1040 40
 

Author
lcy
 
母函数问题:借用模板,加以变形,当然也可以不用模板,也不用背包和dp,直借通过对 那些值得排序相加直到大于等于总值的一半也可以ac
母函数代码如下:
<span style="font-size:14px;">#include<stdio.h>#include<string.h>int a[250100],b[250100];int s[60],t[60];int main(){int n;int i,j,ans1,k,div;while(~scanf("%d",&n),n>=0){int sum=0;for(i=1;i<=n;i++){scanf("%d%d",&s[i],&t[i]);sum+=s[i]*t[i];}div=sum/2;memset(a,0,sizeof(a));memset(b,0,sizeof(b));a[0]=1;for(i=1;i<=n;i++){for(j=0;j<=div;j++)for(k=0;k<=s[i]*t[i]&&k+j<=div;k+=s[i])b[j+k]+=a[j];memcpy(a,b,sizeof(b));memset(b,0,sizeof(b));}for(i=div;i>=0;i--)if(a[i])break;ans1=i;printf("%d %d\n",sum-ans1,ans1);}return 0;}</span>
不用母函数的代码如下:
<span style="font-size:14px;">#include<stdio.h>#include<string.h>#include<math.h>int a[110000];void sort(int a[],int b){int i,j,t;for(i=0;i<b;i++)for(j=i+1;j<b;j++)if(a[i]>a[j])t=a[i],a[i]=a[j],a[j]=t;}int main(){int n;while(~scanf("%d",&n),n>=0){int i,j,num,v,k=0,sum=0;for(i=0;i<n;i++){scanf("%d%d",&v,&num);sum+=v*num;while(num--)a[k++]=v;}sort(a,k);num=0;for(i=k-1;i>=0;i--){if(num+a[i]>sum/2)continue;num+=a[i];}printf("%d %d\n",sum-num,num);}return 0;}</span>
//个人比较倾心于第二种,个人觉得母函数太过于拘泥了

0 0
原创粉丝点击