Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 객체 지향
- 다형성
- inheritance
- enum
- 백준
- 버퍼비우기
- 캡슐화
- 17472
- 추상화
- Encapsulation
- Final
- 최소신장트리
- abstract
- Scanner
- 인터페이스
- java
- polymorphism
- 추상 클래스
- 프림알고리즘
- python
- 열거형
- 와일드카드
- 상속
- this
- 객체지향
- 제네릭
- nextInt
- 내부 클래스
- 생성자
Archives
- Today
- Total
쫑쫑이의 블로그
백준 14003 가장 긴 증가하는 부분 수열 5 Java [LIS] 본문
https://www.acmicpc.net/problem/14003
문제 접근법은 이전에 푼 문제들과 동일하지만
부분 수열을 출력해야한다
A의 요소마다 인덱스값을 저장하는 배열을 만들고
뒤에서부터 값을 하나씩 줄여가면서 size 크기의 배열을 만든다
앞에서부터 만들면 다음 값이 작아지는 구간에 대처하기가 힘들다
뒤에서부터 접근하면 size가 8일때 최초로 등장하는 8미만의 수를 배열에 담아서 출력하면 된다
import java.io.*;
import java.util.*;
public class Main {
static final int MAX_N = 1000000;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = Arrays.stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
int[] D = new int[MAX_N];
D[0] = arr[0];
int[] result = new int[arr.length];
result[0] = 0;
int size = 1;
for (int i = 1; i < N; i++) {
int index = Arrays.binarySearch(D, 0, size, arr[i]);
index = index >= 0 ? index : Math.abs(index) - 1;
D[index] = arr[i];
if (index == size) {
size++;
}
result[i] = index;
}
String[] str = new String[size];
System.out.println(size);
size--;
for(int j = arr.length-1; j >= 0; j--) {
if (size == -1) break;
if (result[j] == size) {
str[size] = String.valueOf(arr[j]);
size--;
}
}
System.out.println(String.join(" ", str));
}
}
배열을 만들때 StringBuilder로 앞에서부터 붙였더니 시간초과로 실패했다
'알고리즘 > 백준' 카테고리의 다른 글
백준 1202 보석 도둑 Java [그리디, 우선순위큐] (1) | 2022.10.24 |
---|---|
백준 17404 RGB거리 2 Java [DP] (0) | 2022.10.23 |
백준 12738 가장 긴 증가하는 부분 수열 3 Java [LIS] (0) | 2022.10.22 |
백준 12015 가장 긴 증가하는 부분 수열 2 Java [LIS] (0) | 2022.10.22 |
백준 1644 소수의 연속합 Java [에라토스테네스의 체] (0) | 2022.10.22 |