Pagini recente » Cod sursa (job #1478842) | Cod sursa (job #66788) | Cod sursa (job #814898) | Cod sursa (job #2182542) | Cod sursa (job #1149767)
#include <cstdio>
using namespace std;
FILE * f=freopen("scmax.in","r",stdin);
FILE * g=freopen("scmax.out","w",stdout);
int a[100001],poz[100001],cmax,i,n,ca,cb;
int x[100001];
int best(int st,int dr)
{
if (st>dr)
return 0;
if (st==dr)
return st;
if (dr-st<=1) return st;
int pivot=(st+dr)/2;
if (x[poz[pivot]]<x[i])
return best(st,pivot);
else if(x[poz[pivot]]>x[i])
return best(pivot,dr);
else
return pivot;
}
int main()
{
scanf("%d",&n);
//f>>n;
for (i=1;i<=n;i++)
scanf("%d",&x[i]);
//f>>x[i];
for(i=n;i>0;i--)
{
ca=x[i];
int aux;
aux=best(1,cmax);
if (aux!=0)
{
while (x[poz[aux]]>x[i])
aux++;
if (x[poz[aux]]<x[i])
poz[aux]=i;
}
else
{
aux=1;
poz[aux]=i;
}
a[i]=poz[aux-1];
cmax=aux>cmax?aux:cmax;
}
cb=poz[cmax];
printf("%d\n",cmax);
//g<<cmax<<'\n';
while (cb)
{
printf("%d ",x[cb]);
//g<<x[cb]<<' ';
cb=a[cb];
}
printf("\n");
//g<<'\n';
fclose(f);
fclose(g);
return 0;
}