-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUVa00437_TheTowerofBabylon.java
69 lines (59 loc) · 1.81 KB
/
UVa00437_TheTowerofBabylon.java
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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 378 (437 - The Tower of Babylon) */
/* SUBMISSION: 11428481 */
/* SUBMISSION TIME: 2013-03-13 04:32:46 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00437_TheTowerofBabylon {
static class Triple implements Comparable<Triple> {
int x, y, z;
public Triple(int x, int y, int z) {
this.x = x; this.y = y; this.z = z;
}
public int compareTo(Triple t) {
if (this.x != t.x) return this.x - t.x;
if (this.y != t.y) return this.y - t.y;
return this.z - t.z;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk;
int t = 1;
while (true) {
int N = Integer.parseInt(in.readLine());
if (N == 0) break;
Triple[] blocks = new Triple[N * 6];
int k = 0;
for (int i = 0; i < N; ++i) {
stk = new StringTokenizer(in.readLine());
int x = Integer.parseInt(stk.nextToken());
int y = Integer.parseInt(stk.nextToken());
int z = Integer.parseInt(stk.nextToken());
blocks[k++] = new Triple(x, y, z);
blocks[k++] = new Triple(x, z, y);
blocks[k++] = new Triple(y, x, z);
blocks[k++] = new Triple(y, z, x);
blocks[k++] = new Triple(z, x, y);
blocks[k++] = new Triple(z, y, x);
}
Arrays.sort(blocks);
int[] dp = new int[N * 6];
int ans = 0;
for (int i = 0; i < N * 6; ++i) {
int max = 0;
for (int j = 0; j < i; ++j)
if (blocks[i].x > blocks[j].x && blocks[i].y > blocks[j].y)
max = Math.max(max, dp[j]);
dp[i] = max + blocks[i].z;
ans = Math.max(ans, dp[i]);
}
System.out.println("Case " + t + ": maximum height = " + ans);
++t;
}
in.close();
System.exit(0);
}
}