Skip to content

Commit c5b508f

Browse files
Create 12 BOJ G2 꼬인 전깃줄.md
1 parent 6a12b13 commit c5b508f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws Exception {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
int n = Integer.parseInt(br.readLine().trim());
8+
9+
int[] arr = new int[n];
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
for (int i = 0; i < n; i++) {
13+
arr[i] = Integer.parseInt(st.nextToken());
14+
}
15+
16+
// LIS using binary search
17+
ArrayList<Integer> lis = new ArrayList<>();
18+
19+
for (int a : arr) {
20+
int pos = Collections.binarySearch(lis, a);
21+
if (pos < 0) pos = -(pos + 1);
22+
23+
if (pos == lis.size()) {
24+
lis.add(a);
25+
} else {
26+
lis.set(pos, a);
27+
}
28+
}
29+
30+
System.out.println(n - lis.size());
31+
}
32+
}

0 commit comments

Comments
 (0)