Big event in HDU
Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 23728 accepted submission (s): 8363
Problem descriptionnowadays, 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 ).
Inputinput 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.
Outputfor 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 shoshould be as equal as possible. at the same time, You shoshould guarantee that A is not less than B.
Sample Input
210 120 1310 1 20 230 1-1
Sample output
20 1040 40/* Primary function solution: Mark the result as 1, find the nearest sum/2. */# include <cstdio> # include <cstring> # include <algorithm> Using STD: Max; const int max = 52; bool c1 [Max x max * 100]; // c2 [Max * max * 100]; int main () {int N; int Val [Max], Count [Max]; while (scanf ("% d", & N), N> 0) {int sum = 0; memset (Val, 0, sizeof (VAL); memset (count, 0, sizeof (count); memset (C1, 0, sizeof (C1); For (INT I = 1; I <= N; I ++) {scanf ("% d", & Val [I], & COUNT [I]); sum + = Val [I] * count [I];} c1 [0] = 1; int Limit = sum> 1; for (INT I = 1; I <= N; I ++) {for (Int J = 0; j <= limit; j ++) {for (int K = 0; j + k <= Limit & K <= Val [I] * count [I]; k + = Val [I]) c1 [K + J] = (c1 [K + J] = 1? C1 [K + J]: C1 [J]); // note that the value cannot be equal to or equal to 50 30 20, V [20] = 0, yes 50}/* For (Int J = 0; j <= limit; j ++) {C1 = c2 [J]; c2 [J] = 0;} */} int K = limit; while (1) {If (c1 [k]) {printf ("% d \ n ", sum-K, k); break;} k -- ;}} return 0 ;}
/* Multi-bag solution: regards both volume and value as value, and the upper limit is sum/2. Find the maximum value, sum-d [sum/2] is the big one */# include <stdio. h> # include <string. h ># include <algorithm> Using STD: Max; const int max = 51; int sum, Count, N; int DP [Max * max * 100]; int V [Max], W [Max], C [Max]; int V [Max], W [Max]; void Div () {// multi-backpack decomposition COUNT = 0; For (INT I = 0; I <n; I ++) {for (Int J = 1; j <= C [I]; j <= 1) {W [count ++] = W [I] * j; C [I]-= J ;} if (C [I]> 0) {W [count ++] = W [I] * C [I] ;}} int main () {While (scanf ("% d", & N), N> 0) {sum = 0; memset (W, 0, sizeof (w); memset (C, 0, sizeof (c); memset (W, 0, sizeof (w); memset (DP, 0, sizeof (DP); For (INT I = 0; I <n; I ++) {scanf ("% d", & W [I], & C [I]); sum + = W [I] * C [I];} int uplimit = sum> 1; Div (); For (INT I = 0; I <count; I ++) {for (Int J = uplimit; j> = W [I]; j --) {DP [J] = max (DP [J], DP [J-W [I] + W [I]);} printf ("% d \ n", Sum-DP [uplimit], DP [uplimit]);} return 0 ;}
Hangdian 1171 big event in HDU (Master function + Multi-backpack solution)