Pagini recente » Cod sursa (job #1424136) | Cod sursa (job #2830376) | Cod sursa (job #2801253) | Unirea 2007, Clasament pentru clasele IX-X | Cod sursa (job #2158141)
#include <iostream>
#include <fstream>
using namespace std;
int a[100010], b[100010], poz[100010], t[100010], c[100010], x=0, st, dr, mij, k, n;
int main()
{
ifstream fin("scmax.in");
ofstream fout("scmax.out");
fin >> n;
for (int i=1; i<=n; i++)
fin >> a[i];
for (int i=1; i<=n; i++)
{
if (a[i]>b[x])
{
b[++x]=a[i];
poz[x]=i;
t[i]=poz[x-1];
}
else
{
st=1;
dr=x;
mij=(st+dr)/2;
while(st<=dr && a[i]!=b[mij])
{
mij=(st+dr)/2;
if (a[i]<b[mij])
dr=mij-1;
else
if (a[i]>b[mij])
st=mij+1;
}
if (a[i]!=b[mij])
{
b[st]=a[i];
t[i]=t[poz[st]];
poz[st]=i;
}
}
}
fout << x << "\n";
c[x]=a[poz[x]];
k=t[poz[x]];
for (int i=x-1; i>=1; i--)
{
c[i]=a[k];
k=t[k];
}
for (int i=1; i<=x; i++)
fout << c[i] << " ";
return 0;
}