-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlis.cpp
54 lines (52 loc) · 1.4 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <vector>
int LIS(vector<int> a){
vector<int> Lis;
Lis.push_back(a[0]);
for (int i=1;i<a.size();i++){
int voy = 0;
int aum = Lis.size();
while (aum)
if (voy+aum<Lis.size() && Lis[voy+aum]<a[i]) voy+=aum;
else aum/=2;
if (voy==0 && a[i]<Lis[voy]) Lis[voy]=a[i];
else if (a[i]<Lis[voy+1]) Lis[voy+1]=a[i];
else if (voy+1==Lis.size() && a[i]>Lis[voy]) Lis.push_back(a[i]);
}
return Lis.size();
}
int LIS_PATH(vector<int> a){
vector<int> Lis;
vector<int> Ind;
vector<int> Path;
Ind.push_back(0);
Path.push_back(-1);
Lis.push_back(a[0]);
for (int i=1;i<a.size();i++){
int voy = 0;
int aum = Lis.size();
while (aum)
if (voy+aum<Lis.size() && Lis[voy+aum]<a[i]) voy+=aum;
else aum/=2;
if (voy==0 && a[i]<Lis[voy]){
Ind[voy]=i;
Lis[voy]=a[i];
Path.push_back(-1);
}
else if (voy+1==Lis.size() && a[i]>Lis[voy]){
Ind.push_back(i);
Lis.push_back(a[i]);
Path.push_back(Ind[Ind.size()-2]);
}
else if (a[i]<Lis[voy+1]){
Ind[voy+1]=i;
Lis[voy+1]=a[i];
Path.push_back(Ind[voy]);
}
}
int x = Ind[Ind.size()-1];
while (x!=-1){
cout<<a[x]<<" ";
x=Path[x];
}
return Lis.size();
}