Pagini recente » Cod sursa (job #2677951) | Cod sursa (job #1580876) | Cod sursa (job #3236337) | Cod sursa (job #2108382) | Cod sursa (job #1090024)
#include <cstdio>
#include <algorithm>
#include <cstring>
const int Q=100000;
int n,v[Q+1],w[Q+1],x[Q+1];
bool cmp(int x, int y)
{
return v[x]<v[y];
}
int arb[Q+1];
int ask(int x)
{
int rez=0;
while(x)
{
if(arb[x]>rez)
rez=arb[x];
x-=x & (-x);
}
return rez+1;
}
void update(int x, int val)
{
while(x<=n)
{
if(arb[x]<val)
arb[x]=val;
x += x &(-x);
}
}
int stiv[Q],vect[Q];
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
scanf("%d",&n);
//int x;
for(int i=1; i<=n; i++)
{
scanf("%d",&v[i]);
w[i]=i;
}
std::sort(w+1,w+n+1,cmp);
for(int i=1; i<=n; i++)
{
if( v[w[i]] == v[w[i-1]] )
{
//printf("?");
x[w[i]]=x[w[i-1]];
}
else
x[w[i]]=i;
}
int max=0,lung,iact;
for(int i=1; i<=n; i++)
{
lung=ask(x[i]-1);
if(lung>max)
{
max=lung;
iact=i;
}
update(x[i],lung);
}
printf("%d\n",max);
int maxx=0,st=0;
for(int i=iact; i>0; i--)
{
if(st==0 || stiv[st] > v[i] )
{
st++;
stiv[st]=v[i];
}
else
{
if(st>maxx)
{
maxx=st;
memcpy(vect, stiv, sizeof stiv);
}
while(v[i]>stiv[st] && st!=0)
{
st--;
}
}
}
if(st>maxx)
{
maxx=st;
memcpy(vect, stiv, sizeof stiv);
}
for(int i=max; i>0; i--)
printf("%d ",vect[i]);
return 0;
}