[BOJ] 14003 가장 긴 증가하는 부분 수열 5

Time Lapse :50min 20sec

14003.cpp

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
#include <stdio.h>
#include <vector>
#include <stack>
using namespace std;
vector<int> vt;
int p[10000001][2], cnt;
int main(){
int n;
scanf("%d",&n);
scanf("%d", &p[0][1]);
vt.reserve(n);
vt.push_back(p[0][1]);
for(int i=1; i<n; i++){
scanf("%d",&p[i][1]);
if(vt[cnt]<p[i][1]) {
vt.push_back(p[i][1]);
p[i][0] = ++cnt;
}
else{
int pos = lower_bound(vt.begin(), vt.end(), p[i][1]) - vt.begin();
vt[p[i][0] = pos] = p[i][1];
}
}
stack<int> s;
for(int i = n - 1; i >= 0 && cnt >= 0; i--) {
if(p[i][0] == cnt) {
s.push(p[i][1]);
--cnt;
}
}
printf("%d\n",s.size());
while(!s.empty()) {
printf("%d ",s.top());
s.pop();
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/02/PS/BOJ/14003/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.