Big Event in Hduproblem descriptionnowadays, we all know that computer College are 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's a trouble thing too. All facilities must go halves. First, all facilities is assessed, and the facilities is thought to being same if they has the same value. It is assumed this there is N (0<n<1000) kinds of facilities (different value, different kinds). Inputinput contains multiple test cases. Each test case is starts with a number n (0 < n <= – 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 this all V is different.
A test case starting with a negative an integer terminates input and this test case was not the be processed. Outputfor, print one line containing, integers A and B which denote the value of computer College and Softwar e College'll get respectively. A and B should be as equal as possible. At the same time, your should guarantee that's A isn't less than B. Sample Input210 1310 1 1-1 Sample Output20 10 40 40 may be their own awareness of the problem is not enough, do not always be the most simple ideas. Then last night and bright elder brother said, he taught me a method, since the search is as far as possible the value of the goods equally, then DP again, and then in the results of DP search, whether there is the total value of the goods can be divided into the results, if there is such a situation, the output is OK. But what Brother Liang told me was wrong in the case of not splitting, well, I went back to the blog. Because in order to get the same situation as possible, in the case that the value of the item is equal to the space occupied by the item, after DP Oh, in the size of total space One-second, dp[half] is as evenly as possible. Because half as much as you can fill.
///go through the DP process first, because you have to judge whether you can divide it anyway,///The DP procedure cannot be broken in the middle process, and will be completed.#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMax_1 =100000+ -;Const intMax_2 = -;intVal[max_2];intNum[max_2];intDp[max_1];intMain () {intN, ans; while(~SCANF ("%d", &N)) {if(N <=0) Break; Memset (DP,0,sizeof(DP)); Ans=0; for(inti =0; I < n; i++) {scanf ("%d%d", Val+i, num+i);///The volume of the goods and their value equivalentsAns + = val[i] *Num[i]; } for(inti =0; I < n; i++) { intK =1; while(K <Num[i]) { for(intj = max_1; J-val[i]*k >=0; j--) Dp[j]= Max (Dp[j], Dp[j-k*val[i]] + k*Val[i]); Num[i]-=K; K*=2; } for(intj = max_1; J-val[i]*num[i] >=0; j--) {Dp[j]= Max (Dp[j], Dp[j-val[i]*num[i]] + num[i]*Val[i]); } }//printf ("%d\n", Dp[max_1]); inthalf = ans/2; if(Dp[half] < ans-Dp[half]) {printf ("%d%d\n", ans-Dp[half], dp[half]); } Else{printf ("%d%d\n", Dp[half],ans-Dp[half]); } } return 0;}
View Code
Reference:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 Const intMax_1 =100000+Ten;7 Const intMax_2 =101;8 intVal[max_2];9 intNum[max_2];Ten intDp[max_1]; One A intMain () - { - intN; the intans, half; - while(~SCANF ("%d", &N)) - { - if(N <=0) + Break; - +Ans =0; AMemset (DP,0,sizeof(DP)); at for(inti =0; I < n; i++) - { -scanf"%d%d", Val+i, num+i); -Ans + = val[i] *Num[i]; - } - inhalf = ans/2; - for(inti =0; I < n; i++) to { + if(Val[i] * Num[i] >=half) - { the for(intj = Val[i]; J <= Half; J + +) * { $DP[J] = max (Dp[j], Dp[j-val[i]] +val[i]);Panax Notoginseng } - //printf ("%d\n", Dp[half]); the } + Else A { the intK =1; + while(K <Num[i]) - { $ for(intj = Half; J-k*val[i] >=0; j--) $ { -DP[J] = max (Dp[j], Dp[j-k*val[i]] + k*val[i]); - } theNum[i]-=K; -K *=2;Wuyi } the - for(intj = Half; J-num[i]*val[i] >=0; j--) Wu { -DP[J] = max (Dp[j], Dp[j-num[i]*val[i]] + num[i]*val[i]); About } $ } - - } - A if(Dp[half] < ans-Dp[half]) + { theprintf"%d%d\n", ans-Dp[half], dp[half]); - } $ Else the { theprintf"%d%d\n", Dp[half],ans-dp[half]); the } the } - return 0; in}
View Code
HDU-1171 Big Event in HDU