-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathlis_dp.cpp
More file actions
45 lines (38 loc) ยท 831 Bytes
/
lis_dp.cpp
File metadata and controls
45 lines (38 loc) ยท 831 Bytes
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
#include <iostream>
#define pb push_back
using namespace std;
int dp[1001];
int arr[1001];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, top = -1, top_i;
cin >> n;
dp[0] = 0; dp[0] = 0;
for(int i=1;i<=n;i++) cin >> arr[i];
for(int i=1;i<=n;i++) {
dp[i] = 1;
for(int j=1;j<=i;j++) {
if(arr[j] < arr[i] && dp[j] +1 > dp[i]) {
dp[i] = dp[j]+1;
}
if(dp[i] > top) {
top = dp[i];
top_i = i;
// LIS๊ธธ์ด ์ต๋๊ฐ์ ๊ฐ๋ ์ธ๋ฑ์ค๋ฅผ top_i์ ์ ์ฅํ๋ค.
}
}
}
cout << top << '\n';
// ์์์ ์ ์ฅํ top_i ์ธ๋ฑ์ค๋ถํฐ ์์ํด์ ๊ฑฐ๊พธ๋ก ํ์ํ๋ฉด LIS๋ฅผ ๊ตฌ์ฑํ๋ ์์๋ค์ ์ถ๋ ฅํ ์ ์๋ค.
for(int i=top_i;i>=1;i--) {
if(dp[i] == top) {
s.push(arr[i]);
top--;
}
}
while(!s.empty()) {
cout << s.top() << ' ';
s.pop();
}
}