Pagini recente » Cod sursa (job #1830854) | Cod sursa (job #2904700) | Cod sursa (job #2457272) | Cod sursa (job #2075727) | Cod sursa (job #2328865)
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int MaxN = 100001, Inf = 0x3f3f3f3f;
int v[MaxN];
int a[MaxN];
int p[MaxN];
int subs[MaxN];
int n, x;
int Lmax;
int Bs(int x);
int main()
{
fin >> n;
v[0] = -Inf;
for (int i = 1; i <= n; ++i)
{
fin >> a[i];
int poz = Bs(a[i]);
v[poz] = a[i];
p[i] = poz;
}
subs[0] = Inf;
fout << Lmax << '\n';
int i = n, k = 0;
for (int L = Lmax; L >= 1; --L)
for (; i >= 1; --i)
if (p[i] == L && a[i] < subs[k])
{
subs[++k] = a[i];
break;
}
for (int i = k; i >= 1; --i)
fout << subs[i] << ' ';
}
int Bs(int x)
{
if (x > v[Lmax]) // sirul nu e strict crescator
{
Lmax++;
return Lmax;
}
int st = 1, dr = Lmax, mj, poz;
while (st <= dr)
{
mj = (st + dr) / 2; // m = (st + (dr - st) / 2)
if (x > v[mj])
st = mj + 1;
else
{
poz = mj;
dr = mj - 1;
}
}
return poz;
}