HDU 1171(非原创)

Big Event in HDU

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


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
 
一开始有点儿难转换为母函数,好像很多牛人都用DP做嘞,速度上应该更快吧。转换为母函数的思路是:把设备排列组合,把所有可能的组合都打出来,然后从总 价值的中间开始搜,只要搜到第一个就是了,再用total-i就是最靠近的两个价值了。。所以输出是total-i,i。这就不用担心总价值是奇数了。写 出母函数的生成函数就可以快速秒掉了。母函数用了1500MS,这样算慢吧。应该比DP要慢很多,但是题目给了5000MS的上限,哈~母函数还是挺伟大滴。
 
 
View Code
 1 #include<iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 const int MAX=55;
 5 const int AMAX=250005;
 6 typedef struct f
 7 {
 8     int val;
 9     int c;
10     int sum;
11 }F;
12 int c1[AMAX],c2[AMAX];
13 int main(void)
14 {
15     F fa[MAX];
16     int n,i,j,k;
17     while(cin>>n)
18     {
19         if(n<0)
20             exit(0);
21         int tal=0;
22         memset(c1,0,sizeof(c1));
23         memset(c2,0,sizeof(c2));
24         for(i=1;i<=n;i++)
25         {
26             cin>>fa[i].val>>fa[i].c;
27             fa[i].sum=fa[i].val*fa[i].c;
28             tal+=fa[i].sum;
29         }
30         c1[0]=1;
31         for(i=fa[1].val;i<=fa[1].sum;i+=fa[1].val)
32             c1[i]=1;
33         for(i=2;i<=n;i++)
34         {
35             for(j=0;j<=tal;j++)
36             {
37                 for(k=0 ; k+j<=tal && k<=fa[i].sum ; k+=fa[i].val)
38                 {
39                     c2[k+j]+=c1[j];
40                 }
41             }
42             for(j=0;j<=tal;j++)
43             {
44                 c1[j]=c2[j];
45                 c2[j]=0;
46             }
47         }
48     //    cout<<c1[40]<<endl;
49         for(i=tal/2;i>=0;i--)//从中间开始搜,用tal-i,不用担心tal为奇数
50         {
51             if(c1[i]!=0)
52             {
53                 cout<<tal-i<<" "<<i<<endl;
54                 break;
55             }
56         }
57     }
58     return 0;
59 }

 

posted on 2012-04-25 08:42  Action!  阅读(808)  评论(0编辑  收藏  举报

导航