-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path481.cpp
More file actions
62 lines (51 loc) · 1.31 KB
/
Copy path481.cpp
File metadata and controls
62 lines (51 loc) · 1.31 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
/*
Author: Andreea Musat
Date: 14 Sept 2017
O(n log k) LIS (where k = length of lis) from CP3
https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=422
*/
#include <bits/stdc++.h>
using namespace std;
// print reversed vector
ostream& operator<<(ostream& os, const vector<int>& v)
{
for (int i = (int)v.size() - 1; i >= 0; i--)
{
os << v[i] << "\n";
}
}
int main()
{
int x, n, crt, pos;
int lis_len = 0, lis_end = 0;
vector<int> arr, lis_path;
while (cin >> x)
{
arr.push_back(x);
}
n = (int)arr.size();
int L[n], L_id[n], P[n];
for (int i = 0; i < n; i++)
{
pos = lower_bound(L, L + lis_len, arr[i]) - L; // pos = length of LIS whose last element can be current element
L[pos] = arr[i]; // arr[i] is now the last element from LIS with pos + 1 elements
L_id[pos] = i; // index of element used as last element for LIS with pos + 1 elements
P[i] = pos ? L_id[pos - 1] : -1; // parent of current element (index of last element of previous sequence)
if (pos + 1 >= lis_len)
{
lis_len = pos + 1;
lis_end = i;
}
}
// reconstruct path by going from last element to its parent
crt = lis_end;
while (P[crt] >= 0)
{
lis_path.push_back(arr[crt]);
crt = P[crt];
}
lis_path.push_back(arr[crt]);
cout << lis_len << "\n-\n";
cout << lis_path;
return 0;
}