include bits stdc define fast cin tie NULL cout tie NULL ios_base sync

 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
#include<bits/stdc++.h>
#define fast cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define ll long long
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
using namespace std;
const int N = 1e5 + 121;
int i,n,kol,l,r,m;
int a[N],b[N],t[N];
int main()
{
fast;
cin>>n;
for(i=1; i<=n; ++i) cin>>a[i];
for(i=n; i>0; --i)
{
if(kol)
{
l=1;
r=kol;
while(l+1<r)
{
m=(r+l) >> 1;
if (t[m]<=a[i]) r=m; else l=m;
}
if (t[r]>a[i])
{
b[i]=t[r];
kol=r+1;
t[kol]=a[i];
} else
if (t[l]>a[i])
{
b[i]=t[l];
kol=l+1;
t[kol]=a[i];
} else
{
kol=1;
t[1]=a[i];
}
} else
{
kol=1;
t[1]=a[i];
}
}
for(i=1; i<n; ++i) cout<<b[i]<<' '; cout<<b[n]<<'\n';
return 0;
}