Pagini recente » Cod sursa (job #2337993) | Cod sursa (job #2292876) | Cod sursa (job #142690) | Cod sursa (job #2004988) | Cod sursa (job #2883159)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
using ll = long long;
const int NMAX(100005);
int best[NMAX], lst[NMAX], ind, rez[NMAX], v[NMAX];
inline void insertX(int val, int pz){
if(val > best[ind]){
++ind;
best[ind] = val;
lst[pz] = ind;
return;
}
int step = 1;
for(;step <= ind; step <<= 1);
step >>= 1;
int poz = 0;
for(; step; step >>= 1)
if(poz + step <= ind && best[poz + step] < val)
poz += step;
++poz;
best[poz] = val;
lst[pz] = ind;
}
int main()
{
int n;
fin >> n;
for(int i = 1; i <= n; ++i){
fin >> v[i];
insertX(v[i], i);
}
int pz = n;
for(int i = ind; i >= 1; --i){
while(lst[pz] != i)
--pz;
rez[i] = v[pz];
}
fout << ind << '\n';
for(int i = 1; i <= ind; ++i)
fout << rez[i] << ' ';
return 0;
}