Pagini recente » Cod sursa (job #1598007) | Cod sursa (job #1192813) | Cod sursa (job #1912176) | Cod sursa (job #975232) | Cod sursa (job #1224072)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAXN 100005
using namespace std;
int n, a[MAXN], stiva[MAXN], best[MAXN], previ[MAXN];
int poz, nr;
void citire()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
}
void afisare(int p)
{
if (p == -1)
return;
afisare(previ[p]);
printf("%d ", a[p]);
}
int place(int x)
{
int i, step;
//nr++;
for (step = 1; step < nr; step <<= 1);
for (i = 0; step; step >>= 1)
{
if (i + step < nr && a[stiva[i + step]] <= x)
i += step;
}
if (a[stiva[i]] < x)
i++;
return i;
//nr--;
/*vector<int> v;
for (int i = 0; i < nr; i++)
v.push_back(a[stiva[i]]);
return lower_bound(v.begin(), v.end(), x) - v.begin();*/
}
void sol()
{
for (int i = 0; i < n; i++)
{
poz = place(a[i]);
stiva[poz] = i;
best[i] = poz + 1;
previ[i] = (poz == 0 ? -1 : stiva[poz-1]);
if (poz + 1 > nr)
nr = poz + 1;
}
int maxim = 0, p = 0;
for (int i = 0; i < n; i++)
if (best[i] > maxim)
{
maxim = best[i];
p = i;
}
printf("%d\n", maxim);
afisare(p);
}
int main()
{
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
citire();
sol();
return 0;
}