Pagini recente » Cod sursa (job #2387914) | Cod sursa (job #2032868) | Cod sursa (job #2813488) | Cod sursa (job #786856) | Cod sursa (job #1962770)
#include <iostream>
#include <fstream>
using namespace std;
int a[100010], poz[100010], t[100010], b[100010], c[100010];
int main()
{
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
int n, i, j, max, x=0, k=0, mij, li, lf, aux;
fin >> n;
for (i=1; i<=n; i++)
fin >> a[i];
for (i=1; i<=n; i++)
{
if (i==1)
{
b[++x]=a[i];
t[i]=0;
poz[x]=1;
}
else
{
if (a[i]>b[x])
{
b[++x]=a[i];
poz[x]=i;
t[i]=poz[x-1];
}
else
{
li=1;
lf=x;
mij=(li+lf)/2;
while (li<=lf && b[mij]!=a[i])
{
if (a[i]<b[mij])
lf=mij-1;
else
li=mij+1;
mij=(li+lf)/2;
}
if (b[mij]==a[i])
{
poz[mij]=i;
t[i]=poz[mij-1];
}
else
{
b[li]=a[i];
poz[li]==i;
t[i]=poz[li-1];
}
}
}
aux=poz[x];
while (t[aux])
{
c[++k]=a[aux];
aux=t[aux];
}
c[++k]=a[aux];
for (i=k; i>=1; i--)
fout << c[i] << " ";
}
return 0;
}