Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- acm
- backtracking
- 이분탐색
- 재귀
- 분할정복
- union-find
- 세그먼트트리
- BFS
- 알고리즘문제해결전략
- 스택
- Algospot
- 동적계획법
- 종만북
- 분리집합
- 백트래킹
- Greedy
- 알고스팟
- 다이나믹프로그래밍
- 누적합
- 그리디
- priority_queue
- DP
- BOJ
- 완전탐색
- 문자열
- 너비우선탐색
- 백준
- stack
- DFS
- 유니온파인드
Archives
- Today
- Total
DAMPER's 낙서장
14003 가장 긴 증가하는 부분 수열 5 본문
728x90
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
수열의 최대 크기가 1,000,000 인 LIS 문제이다.
LIS 의 크기만 구하는 문제가 아닌, 정답이 될 수 있는 LIS 하나 출력하는 문제.
\( O(NlogN) \) LIS 알고리즘 (이분탐색) + 넣을 때마다 인덱싱 저장으로 문제를 해결할 수 있었음.
/*
수정 중
*/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#include <bits/stdc++.h>
using namespace std;
#define swap(a,b) (a)^=(b)^=(a)^=(b)
#define endl '\n'
#define int long long
using pii = pair<int, int>;
const double EPS = 1e-9;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int _lower_bound(int start, int end, int key, vector<int>& v)
{
while(start < end)
{
int mid = (start+end)/2;
if(key > v[mid]) start = mid+1;
else end = mid;
}
return end+1;
}
int32_t main()
{
cin.sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v(n, 0);
for(int i=0;i<n;i++)
cin >> v[i];
vector<int> lis(n, 0), pos(n, 0);
int lisp = 0;
lis[lisp] = v[0];
pos[0] = 0;
for(int i=1;i<n;i++)
{
if(lis[lisp] < v[i])
{
lis[++lisp] = v[i];
pos[i] = lisp;
}
else
{
int k = _lower_bound(0, lisp, v[i], lis);
lis[k-1] = v[i];
pos[i] = k-1;
}
}
cout << lisp+1 << endl;
int k = lisp;
vector<int> ans;
for(int i=n-1;i>=0;i--)
{
if(pos[i] == k)
{
ans.push_back(v[i]);
k--;
}
}
reverse(ans.begin(), ans.end());
for(auto &K : ans)
cout << K << ' ';
cout << endl;
return 0;
}
|
cs |
728x90
'Problem Solving > BOJ 문제풀이' 카테고리의 다른 글
4386. 별자리 만들기 (0) | 2022.01.03 |
---|---|
17425. 약수의 합 (0) | 2022.01.01 |
1208 부분수열의 합 2 (0) | 2021.08.20 |
9252 LCS 2 (0) | 2021.08.20 |
12100 2048 (Easy) (0) | 2021.08.18 |