Algorithm/백준
[백준] 1965 상자넣기
SSONGMI
2021. 4. 14. 00:22
반응형
문제 출처: www.acmicpc.net/problem/1965
1965번: 상자넣기
정육면체 모양의 상자가 일렬로 늘어서 있다. 상자마다 크기가 주어져 있는데, 앞에 있는 상자의 크기가 뒤에 있는 상자의 크기보다 작으면, 앞에 있는 상자를 뒤에 있는 상자 안에 넣을 수가
www.acmicpc.net
: 일반적인 최장 증가 부분 수열과 같다.
: 다만 동일한 수가 나올 수 있기 때문에 바이너리 서치를 할 때 나오는 인덱스가 양수일 때와 음수일 때를 고려해주어야 한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class 상자넣기 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
public static void main(String[] args) throws NumberFormatException, IOException {
int N = Integer.parseInt(br.readLine());
int[] LIS = new int[N];
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int size = 0;
for(int i = 0; i < N; i++) {
int idx = Arrays.binarySearch(LIS, 0, size, arr[i]);
if(idx < 0)idx = Math.abs(idx)-1;
LIS[idx] = arr[i];
if(idx == size)++size;
}
System.out.println(size);
}
}
반응형