티스토리 뷰
https://www.acmicpc.net/problem/14003
14003번: 가장 긴 증가하는 부분 수열 5
첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (-1,000,000,000 ≤ Ai ≤ 1,000,000,000)
www.acmicpc.net
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v => v.split(' ').map(Number))
const [N] = input[0];
const arr = input[1];
function binarySearch(left, right, target) {
let mid;
while (left < right) {
mid = Math.floor((left + right) / 2);
if (lis[mid] < target) {
left = mid + 1;
} else {
right = mid
}
}
return right;
}
let lis = [];
let records = [];
records[0] = 0;
let j = 0;
lis[0] = arr[0];
let i = 1;
while (i < N) {
if (lis[j] < arr[i]) {
lis[++j] = arr[i];
records[i] = j;
} else {
let idx = binarySearch(0, j, arr[i]);
lis[idx] = arr[i]
records[i] = idx;
}
i++;
}
let answer = [];
let max = Math.max(...records)
const maxIdx = records.indexOf(max)
for (let i = maxIdx; i >= 0; i--) {
if (records[i] == max) {
answer.push(arr[i]);
max--;
}
if (max < 0) break;
}
console.log(answer.length)
console.log(answer.reverse().join(' '))
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 11722번: 가장 긴 감소하는 부분 수열 (0) | 2022.04.24 |
---|---|
Node.js) 백준 11053번: 가장 긴 증가하는 부분 수열 (0) | 2022.04.24 |
Node.js)백준 1561번: 놀이공원 (0) | 2022.04.23 |
Node.js) 백준 14627번: 파닭파닭 (0) | 2022.04.22 |
Node.js) 백준 16434번 드래곤 앤 던전 (0) | 2022.04.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 동적프로그래밍
- 투포인터 연습
- 다이나밍프로그래밍
- 롱베케이션
- MOD
- create databases;
- 개발자면접
- DB 생성
- 서버점검
- 로드나인
- 면접질문
- node.js
- KMP
- BFS
- 최소공통조상
- 면접비
- 은둔청년체험
- MySQL
- 투포인터
- 서버개발
- create db
- 그래프
- 다이나믹프로그래밍
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함