-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2655.java
More file actions
131 lines (117 loc) · 3.83 KB
/
Copy path2655.java
File metadata and controls
131 lines (117 loc) · 3.83 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// https://www.acmicpc.net/problem/2655
// 가장 높은 탑 쌓기
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, dp[], top[];
static Brick bricks[];
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(reader.readLine());
bricks = new Brick[n];
for(int i=0; i<n; i++)
bricks[i] = new Brick(i, reader.readLine());
// sort
Brick[] bottomSort = bricks.clone();
Arrays.sort(bottomSort, new Comparator<Brick>(){
@Override
public int compare(Brick o1, Brick o2){
return o1.bottom - o2.bottom;
}
});
Brick[] weightSort = bricks.clone();
Arrays.sort(weightSort, new Comparator<Brick>(){
@Override
public int compare(Brick o1, Brick o2){
return o1.weight - o2.weight;
}
});
// solution
dp = new int[n];
top = new int[n];
int max = 0, ansIdx = -1;
for(int i=0; i<n; i++){
if(dp[bottomSort[i].num] == 0){
int num = getMaxDpNum(bottomSort, i-1, bottomSort[i]);
top[bottomSort[i].num] = num;
dp[bottomSort[i].num] = (num==-1 ? 0 : dp[num]) + bottomSort[i].height;
if(dp[bottomSort[i].num] > max){
max = dp[bottomSort[i].num];
ansIdx = bottomSort[i].num;
}
}
if(dp[weightSort[i].num] == 0){
int num = getMaxDpNum(weightSort, i-1, weightSort[i]);
top[weightSort[i].num] = num;
dp[weightSort[i].num] = (num==-1 ? 0 : dp[num]) + weightSort[i].height;
if(dp[weightSort[i].num] > max){
max = dp[weightSort[i].num];
ansIdx = weightSort[i].num;
}
}
}
// output
int count = 0;
StringBuilder sb = new StringBuilder();
while(ansIdx != -1){
count++;
sb.insert(0, ansIdx+1+"\n");
ansIdx = top[ansIdx];
}
sb.insert(0, count+"\n");
System.out.print(sb);
// debugging
/*System.out.println();
print(bricks);
print(bottomSort);
print(weightSort);
print(dp);
print(top);
System.out.println(max+", "+ansIdx); */
}
public static int getMaxDpNum(Brick bricks[], int end, Brick below){
int max = 0, num = -1;
for(int i=0; i<=end; i++){
if(!below.isPutAble(bricks[i])) continue;
if(dp[bricks[i].num] > max){
max = dp[bricks[i].num];
num = bricks[i].num;
}
}
return num;
}
public static void print(Brick[] arr){
for(int i=0; i<arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void print(int[] arr){
for(int i=0; i<arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
class Brick{
int num, bottom, height, weight;
public Brick(int n, int b, int h, int w){
num = n;
bottom = b;
height = h;
weight = w;
}
public Brick(int n, String input){
num = n;
StringTokenizer st = new StringTokenizer(input);
bottom = Integer.parseInt(st.nextToken());
height = Integer.parseInt(st.nextToken());
weight = Integer.parseInt(st.nextToken());
}
public boolean isPutAble(Brick top){
return this.bottom > top.bottom && this.weight > top.weight;
}
@Override
public String toString(){
return "("+num+": "+bottom+", "+height+", "+weight+")";
}
}