Pagini recente » Cod sursa (job #1163143) | Cod sursa (job #831048) | Cod sursa (job #1547669) | Cod sursa (job #2807537) | Cod sursa (job #1968307)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
#define pb push_back
typedef long long ll;
const int NMax = 1e5 + 5;
int N,nrSol,nrRev;
int v[NMax],sol[NMax],pre[NMax],rev[NMax];
int main() {
in>>N;
for (int i=1;i<=N;++i) {
in>>v[i];
int pw=1,mij,poz = 0;
for (;pw<=nrSol;pw <<= 1) ;
pw >>= 1;
while (pw) {
if (poz+pw <= nrSol && v[sol[poz+pw]] <= v[i]) {
poz += pw;
}
pw >>= 1;
}
if (v[sol[poz]] == v[i]) {
continue;
}
sol[poz+1] = i;
pre[i] = sol[poz];
if (poz == nrSol) {
++nrSol;
}
}
out<<nrSol<<'\n';
int aux = sol[nrSol];
while (aux != 0) {
rev[++nrRev] = v[aux];
aux = pre[aux];
}
while (nrRev) {
out<<rev[nrRev--]<<' ';
}
in.close();out.close();
return 0;
}