-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCIS.cpp
More file actions
70 lines (53 loc) · 1.24 KB
/
LCIS.cpp
File metadata and controls
70 lines (53 loc) · 1.24 KB
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
// https://codeforces.com/contest/10/problem/D
#include <iostream>
#include <vector>
using namespace std;
int N;
int M;
int dp[551][551], path[551][551], arr1[551], arr2[511];
bool print (int x) {
if (!x) {
return false;
}
if (print(path[N][x])) {
cout << arr2[x] << " ";
} else {
cout << arr2[x] << " ";
}
return true;
}
int main ()
{
cin >> N;
for (int i = 1; i <= N; i++) cin >> arr1[i];
cin >> M;
for (int i = 1; i <= M; i++) cin >> arr2[i];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= N; i++) {
int count = 0;
int posit = 0;
for (int j = 1; j <= M; j++) {
dp[i][j] = dp[i-1][j];
path[i][j] = path[i-1][j];
if (arr1[i] == arr2[j] && count+1 > dp[i][j]) {
dp[i][j] = count+1;
path[i][j] = posit;
}
if (arr1[i] > arr2[j] && dp[i-1][j]>count) {
count = dp[i-1][j];
posit = j;
}
}
}
int fin = 1;
for (int i = 1; i <= M; i++) {
if (dp[N][i] > dp[N][fin]) {
fin = i;
}
}
cout << dp[N][fin] << "\n";
if (dp[N][fin]) {
print(fin);
cout << "\n";
}
}