http://acm.hdu.edu.cn/showproblem.php?pid=1171Big Event in HDU
Time limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 28483 Accepted Submission (s): 10027
Problem 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, you should guarantee, which A is not less than B.
Sample Input210 120 1310 1 20 230 1-1
Sample OUTPUT20 1040 40
Test instructions: The problem is a multiple backpack, B's maximum capacity is the total value/2;
Useful female functions available online or in a purely violent manner, mark each of the attainable states as 1, and finally find the nearest maximum capacity, for example, the second case can only find the possible states less than the maximum capacity, and 10 20 30 40
The idea applied to the backpack is to turn multiple backpacks into 0 1 backpacks,
• Article I items can be divided into q[i] pieces of goods, the value of W[i], 2*w[i], 3*w[i], ..., the cost is c[i], 2*c[i], 3*c[i], ...
The k item I synthesize an item
Optimization
• Combine k item I with an item • Keep only k= 1, 2, 4, ..., 2^ (p-1), q[i]-2^p+1 items • Where p is the largest integer that satisfies q[i]-2^p+1>0 • This ensures that the combination of these items can be taken from 1 to Q[i] And certainly not more than q[i] but multiple backpacks have a faster template
1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 #defineN 556 7 intVal;8 intf[n* the];9 Ten //each item can only be used once One voidOnezeropack (intVintc) A { - intJ; - for(J=val; j>=v; j--) the { -F[j]=max (f[j-v]+c,f[j]); - } - } + //Each item can be used indefinitely - voidCompletepack (intVintc) + { A intJ; at for(J=v; j<=val; J + +) - { -F[j]=max (f[j-v]+c,f[j]); - } - } - //Limited use of each item in voidMultiplepack (intVintCintnum) - { to if(c*num>=val) + { - Completepack (v,c); the return; * } $ intk=1;Panax Notoginseng while(k<num) - { theOnezeropack (k*v,k*c); +num=num-K; Ak=k*2; the } +Onezeropack (num*v,num*c); - } $ $ - intV[n], num[n]; - intMain () the { - intN;Wuyi while(~SCANF ("%d", &n), n >=0) the { - for(inti =0; I < n; i++) scanf ("%d%d", &v[i], &num[i]); Wuval =0; - for(inti =0; I < n; i++) val + = v[i]*Num[i]; About intTM =Val; $Val/=2; -Memset (F,0,sizeof(f)); - for(inti =0; I < n; i++) Multiplepack (V[i], v[i], num[i]); -printf"%d%d\n", tm-F[val], f[val]); A } + return 0; the}
Big Event in HDU (multi-pack template)