-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11003_boxes_v2.c
70 lines (56 loc) · 1.67 KB
/
11003_boxes_v2.c
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
/*
* Author: Chen Rushan
* E-Mail: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN(x, y) (x) < (y) ? (x) : (y)
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define MAX_N_BOX 1002
#define MAX_CAPACITY 3001
short height[MAX_CAPACITY][MAX_N_BOX];
short weight[MAX_N_BOX];
short capacity[MAX_N_BOX];
int N, max_cap;
void
DP(void)
{
int c = 0, i = 0;
for (c = 0; c <= max_cap; c++)
height[c][N] = 0;
for (i = N - 1; i >= 1; i--) {
for (c = 0; c <= max_cap; c++) {
if (c < weight[i]) {
height[c][i] = height[c][i + 1];
} else {
int cap = MIN(capacity[i], c - weight[i]);
height[c][i] = MAX(1 + height[cap][i + 1],
height[c][i + 1]);
}
}
}
}
int
main(int argc, char **argv)
{
while (1) {
int i = 0;
scanf("%d", &N);
if (N == 0)
break;
max_cap = 0;
for (i = 0; i < N; i++) {
scanf("%d %d", weight + i, capacity + i);
if (max_cap < capacity[i])
max_cap = capacity[i];
}
DP();
int maxh = 0;
maxh = 1 + height[capacity[0]][1];
for (i = 1; i < N; i++)
maxh = MAX(maxh, 1 + height[capacity[i]][i + 1]);
printf("%d\n", maxh);
}
return 0;
}