hdu 1171 big event in hdu
2011-10-16 10:15
483 查看
Big Event in HDU
[b]Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9282 Accepted Submission(s): 3220
[/b]
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,都搞不清了。
重点是通过这道题发现了 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;
}
相关文章推荐
- HDU 1171 Big Event in HDU
- 【杭电oj】1171 - Big Event in HDU(01背包)
- HDU 1171 Big Event in HDU
- HDU 1171 Big Event in HDU 动态规划多重背包
- 动态规划J - Big Event in HDU HDU - 1171
- HDOJ-1171-Big Event in HDU 解题报告
- HDU-1171-Big Event in HDU(多重背包 二进制优化)
- HDU 1171 Big Event in HDU 多重背包二进制优化
- HDU - 1171 Big Event in HDU(多重背包或BFS)
- hdu 1171 Big Event in HDU
- HDoj-1171-Big Event in HDU-母函数
- hdu 1171 Big Event in HDU
- hdu 1171 Big Event in HDU (母函数)
- HDU 1171 Big Event in HDU(01背包)
- HDU 1171 Big Event in HDU(多重背包)
- DP Problem R:Big Event in HDU(HDU 1171)
- HDU-1171-Big Event in HDU(01背包的简单变形)
- HDU 1171 Big Event in HDU
- 【HDU】1171 - Big Event in HDU(母函数)
- hdoj-1171-Big Event in HDU