Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9282 Accepted Submission(s): 3220
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).
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.
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,都搞不清了。
重点是通过这道题发现了 HDU 的 OJ 是先判输出匹配再判主程序返回值的。
一开始,用的是小数组和库函数 assert,连续 WA
// ...
assert (...);
static bool g [6000];
// ...
后来把库函数 assert 换成自定义 Assert
void Assert (bool b) {
if (! b) {
while (true) {}
}
}
出现了 TLE。
把数组开大点, AC 了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
void Assert (bool b) {
if (! b) {
while (true) {}
}
}
void Solve () {
static int v [60]; // v of the problem
static int m [60]; // m of the problem
int n; // n of the problem
int total = 0;
if (scanf ("%d", &n) == EOF || n < 0) {
exit (0);
}
Assert (n < 60);
for (int i=0; i<n; ++i) {
scanf ("%d%d", &v[i], &m[i]);
total += v[i] * m[i];
}
int half = total / 2;
Assert (half < 150000);
static bool g [150000]; // generator
memset (g, 0, sizeof (g));
g [0] = true;
for (int i=0; i<n; ++i) {
for (int h=half; h>=0; --h) {
if (! g [h]) {
continue;
}
for (int k=1; k<=m[i] && h+k*v[i]<=half; ++k) {
g [h+k*v[i]] = true;
}
}
#ifdef _DEBUG
for (int h=0; h<=half; ++h) {
printf ("%d%s", g [h], h==half ? "\n" : " ");
}
#endif
}
int b = half + 1;
while (--b >= 0 && ! g [b]) {}
printf ("%d %d\n", total - b, b);
}
int main () {
while (true) {
Solve ();
}
return 0;
}