Pagini recente » Cod sursa (job #473332) | Cod sursa (job #1620057) | Cod sursa (job #1257288) | Cod sursa (job #166399) | Cod sursa (job #1096880)
#include<cstdio>
#define NMAX 100005
#define oo (2000000005)
using namespace std;
int N,v[NMAX],best[NMAX],pred[NMAX];
void Read()
{
freopen("scmax.in","r",stdin);
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&v[i]);
}
void TSFH()
{
best[1]=1;
v[N+1]=oo;
int i,j,bmax;
for(i=1;i<=N+1;i++)
{
bmax=0;
for(j=1;j<i;j++)
if(v[j]<v[i] && best[j]>best[bmax])
bmax=j;
if(bmax)
{
best[i]=best[bmax]+1;
pred[i]=bmax;
}
else
{
best[i]=1;
pred[i]=0;
}
}
}
void rec(int i)
{
if(best[i])
{
rec(pred[i]);
printf("%d ",v[i]);
}
}
void Print()
{
freopen("scmax.out","w",stdout);
printf("%d\n",best[N+1]-1);
rec(pred[N+1]);
}
int main()
{
Read();
TSFH();
Print();
}