Pagini recente » Cod sursa (job #330279) | Cod sursa (job #800710) | Cod sursa (job #496539) | Cod sursa (job #812321) | Cod sursa (job #1193137)
#include <cstdio>
#include <algorithm>
using namespace std;
#define NMAX 100001
#define max(a,b) ((a<b) ? b : a)
int A[NMAX],Q[NMAX],P[NMAX],sol[NMAX];
int maxSol,maxLg,L,R,M,nowP,i,N;
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
scanf("%d",&N);
for (i=1;i<=N;++i) scanf("%d",&A[i]);
Q[1]=A[1];
maxLg=P[1]=1;
for (i=2;i<=N;++i)
{
L=1;
R=maxLg;
nowP=0;
while (L<=R && !nowP)
{
M=(L+R)/2;
if (Q[M]==A[i])
{
nowP=M;
continue;
}
(Q[M]>A[i]) ? R=M-1 : L=M+1;
}
if (nowP==0) nowP=L;
Q[nowP]=A[i];
maxLg=max(maxLg,L);
P[i]=nowP;
maxSol=max(maxSol,P[i]);
}
for (i=1;i<=N;++i) if (P[i]==maxSol) nowP=i;
sol[1]=A[nowP];
sol[0]=1;
maxLg=P[nowP];
for (i=nowP-1;i>=1;i--)
{
if (P[i]!=maxLg-1) continue;
--maxLg;
sol[++sol[0]]=A[i];
}
printf("%d\n",sol[0]);
for (i=sol[0];i>=1;--i) printf("%d ",sol[i]);
return 0;
}