Big Event in HDU
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
Author
lcy
题目类型:多重背包
算法分析:dp[j]表示选择前i种家居恰好花费j所具有的最大价值。这里将总价值平分得到val,然后以val为背包容量来跑一个完全背包,最后从val到0找到满足dp[i] == i的最大的i,则i和all - i就是答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/************************************************* Author :supermaker Created Time :2015/11/23 20:45:37 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e6 + 6666; int dp[maxn], v[66], m[66]; void PP (int V, int c, int num) { if (c * num >= V) { for (int i = c; i <= V; i++) dp[i] = max (dp[i], dp[i-c] + c); } int k = 1; while (k < num) { for (int i = V; i >= k * c; i--) dp[i] = max (dp[i], dp[i-k*c] + k * c); num -= k; k <<= 1; } for (int i = V; i >= num * c; i--) dp[i] = max (dp[i], dp[i-num*c] + num * c); } int main() { //CFF; //CPPFF; int n; while (scanf ("%d", &n) != EOF) { if (n < 0) break; int all = 0; for (int i = 1; i <= n; i++) { scanf ("%d%d", &v[i], &m[i]); all += v[i] * m[i]; } memset (dp, 0, sizeof (dp)); int tt = all / 2; for (int i = 1; i <= n; i++) PP (tt, v[i], m[i]); int resa, resb; for (int i = tt; i >= 0; i--) if (dp[i] == i) { resa = i; resb = all - i; break; } printf ("%d %d\n", max (resa, resb), min (resa, resb)); } return 0; } |
- « 上一篇:hdu1203
- hdu2844:下一篇 »