Pagini recente » Cod sursa (job #576724) | Cod sursa (job #786983) | Cod sursa (job #451336) | Statistici Anghel Livia (liv52) | Cod sursa (job #2874751)
#include <bits/stdc++.h>
using namespace std;
#define DIM 100005
int N, ts, x;
int v[DIM], poz[DIM], pre[DIM], cb[DIM];
int bin_search(int val)
{
int pw = 1;
while (pw <= ts){
pw <<= 1;
}
pw >>= 1;
int pos = 0;
while (pw){
if (pos + pw <= ts){
if (cb[pos + pw] < val){
pos += pw;
}
}
pw >>= 1;
}
return pos;
}
void solve(int pz)
{
if (pz == 0) return;
solve(pre[pz]);
cout << v[pz] << ' ';
}
int main()
{
freopen ("scmax.in", "r", stdin);
freopen ("scmax.out", "w", stdout);
scanf("%d\n", &N);
for (int i = 1; i <= N; i++){
scanf("%d", &v[i]);
}
int mxpos = 1;
for (int i = 1; i <= N; ++i){
int pos = bin_search(v[i]);
if(pos == ts){
cb[++ts] = v[i];
poz[ts] = i;
pre[i] = poz[pos];
mxpos = i;
}
else {
pre[i] = poz[pos];
if (cb[pos + 1] > v[i]){
cb[pos + 1] = v[i];
poz[pos + 1] = i;
}
}
}
cout << ts << '\n';
solve(mxpos);
cout << '\n';
return 0;
}