-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2352Faster.java
More file actions
54 lines (48 loc) · 1.12 KB
/
BOJ2352Faster.java
File metadata and controls
54 lines (48 loc) · 1.12 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
package quki.algorithm.dp;
import java.util.ArrayList;
import java.util.Scanner;
public class BOJ2352Faster {
static int findSameorBig(ArrayList<Integer> T, int target) {
int ans = 0;
int left = 0;
int right = T.size()-1;
while (left <= right) {
int mid = (left + right) / 2;
if (T.get(mid) > target) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int In[] = new int[n];
for(int i = 0; i<n;i++){
In[i] = sc.nextInt();
}
ArrayList<Integer> T = new ArrayList<>();
int result[] = new int[n];
T.add(In[0]);
for(int i = 1;i<n;i++){
if(T.get(T.size()-1) < In[i]){
T.add(In[i]);
result[i] = In[i];
} else if (T.get(0) > In[i]){
T.remove(0);
T.add(0, In[i]);
} else {
int idx = findSameorBig(T, In[i]);
T.remove(idx);
T.add(idx, In[i]);
result[i] = T.get(idx-1);
}
}
for(int e: result){
System.out.println(e);
}
}
}