Cod sursa(job #886665)

Utilizator matemariaescuMaria Mateescu matemariaescu Data 23 februarie 2013 09:38:09
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
# include <cstdio>
# define NMAX 100100
using namespace std;

int n,i,pz,lg;
int a[NMAX],bst[NMAX],poz[NMAX];

void det(int i, int v)
{
  if (v<=0) return;
  while (poz[i]!=v) i--;
  det(i-1,v-1);
  printf("%d ",a[i]);
}

int main ()
{
  freopen("scmax.in","r",stdin);
  freopen("scmax.out","w",stdout);
  scanf ("%d",&n);
  lg=0;
  for (i=1;i<=n;i++)
  {
    scanf("%d",&a[i]);
    pz=lg+1;
    while (pz>0&&bst[pz-1]>=a[i])
      pz--;
    if (bst[pz]<a[i])
      lg++,bst[lg]=a[i],poz[i]=lg;
    else
      bst[pz]=a[i],poz[i]=pz;
  }
  printf("%d\n",lg);
  det(n,lg);
  printf("\n");
  return 0;
}