-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS.cpp
40 lines (32 loc) · 805 Bytes
/
LIS.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
37
38
39
40
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define N 100005
int n, a[N], F[N];
int m, b[N];
int main() {
// array a, length n
while (scanf("%d", &a[n + 1]) > 0) n++;
// array b, length m, in increasing order
for (int i = 1; i <= n; i++) {
F[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
m = max(m, F[i]);
b[F[i]] = a[i];
}
// m is also max(F)
printf("%d\n-\n", m);
int Expected = m;
vector<int> T;
// add m elements from a into T
for (int i = n; i >= 1; i--) {
if (F[i] == Expected) {
T.push_back(a[i]);
Expected--;
}
}
// output T in reversed order
for (int i = T.size() - 1; i >= 0; i--)
printf("%d\n", T[i]);
}