코드와이
[BAEKJOON] 2631. 줄 세우기 본문
문제링크
https://www.acmicpc.net/problem/2631
2631번: 줄세우기
KOI 어린이집에는 N명의 아이들이 있다. 오늘은 소풍을 가는 날이다. 선생님은 1번부터 N번까지 번호가 적혀있는 번호표를 아이들의 가슴에 붙여주었다. 선생님은 아이들을 효과적으로 보호하기
www.acmicpc.net
백준 문제 '가장 긴 증가부분 수열' 문제와 유사한 문제
package acmicpc.Gold5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class 줄_세우기 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Integer> list = new ArrayList<>();
list.add(-1);
for(int i = 0 ; i < n ; i++) {
int x = Integer.parseInt(br.readLine());
if(list.get(list.size() - 1) < x) list.add(x);
else {
int low = 0;
int high = list.size() - 1;
while(low < high) {
int mid = (low + high) / 2;
if(list.get(mid) > x) {
high = mid;
} else {
low = mid + 1;
}
}
list.set(low, x);
}
}
System.out.println(n - list.size() + 1);
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 14226. 이모티콘 (0) | 2021.07.13 |
---|---|
[BAEKJOON] 2470. 두 용액 (0) | 2021.07.09 |
[BAEKJOON] 11559. Puyo Puyo (0) | 2021.07.06 |
[BAEKJOON] 5557. 1학년 (0) | 2021.07.06 |
[BAEKJOON] 2636. 치즈 (0) | 2021.07.06 |