-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBABTWR.cpp
More file actions
89 lines (71 loc) · 1.66 KB
/
BABTWR.cpp
File metadata and controls
89 lines (71 loc) · 1.66 KB
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
struct block
{
int area;
int height;
int bx, by;
bool operator < (const block &bl) const
{
return (area > bl.area);
}
} BLOCKS[3 * 30];
int block_count;
map<int, int> cache;
// what is the tallest tower we can make, so
// that it ends with block_id?!
int solve(int block_id)
{
map<int, int>::iterator it = cache.find(block_id);
if(it != cache.end()) return it->second;
int best = 0;
bool init = false;
for(int k = block_id - 1; k >= 0; k--){
if((BLOCKS[k].bx > BLOCKS[block_id].bx
&& BLOCKS[k].by > BLOCKS[block_id].by))
{
int ts = solve(k);
if(!init){
best = ts;
init = true;
}
best = max(best, ts);
}
}
return cache[block_id] = BLOCKS[block_id].height + best;
}
int main() {
while(scanf("%d", &block_count) == 1 && block_count)
{
for(int j = 0, k = 0; j < block_count; j++, k += 3){
int width, height, depth;
scanf("%d %d %d", &height, &width, &depth);
// generate 3 rotations of block.
BLOCKS[k].area = width * height;
BLOCKS[k].height = depth;
BLOCKS[k].bx = max(width, height);
BLOCKS[k].by = min(width, height);
BLOCKS[k+1].area = width * depth;
BLOCKS[k+1].height = height;
BLOCKS[k+1].bx = max(width, depth);
BLOCKS[k+1].by = min(width, depth);
BLOCKS[k+2].area = height * depth;
BLOCKS[k+2].height = width;
BLOCKS[k+2].bx = max(depth, height);
BLOCKS[k+2].by = min(depth, height);
}
block_count *= 3;
sort(BLOCKS, BLOCKS + block_count);
int best;
bool init = false;
cache.clear();
for(int j = 0; j < block_count; j++){
int ts = solve(j);
if(!init){
best = ts;
init = true;
}
best = max(best, ts);
}
printf("%d\n", best);
}
return 0;
}