Cod sursa(job #897215)

Utilizator kiralalaChitoraga Dumitru kiralala Data 27 februarie 2013 19:23:55
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<fstream>
#define nmax 100003
using namespace std;
ifstream f("scmax.in");
ofstream o("scmax.out");
int best[nmax],a[nmax], maxi,sol=0,p;
int q[nmax],sf,poz[nmax];
int n;
int k;
void read()
{
    f>>n;
    for(int i=1; i<=n; ++i) f>>a[i];
}

int bs(int q[], int x)
{
    int i=1,j=sf+1,ok=0;
    if(j==1) return 0;
    while(i<j)
    {
        int mij=(i+j)/2;
        if(q[mij]>=x)
            j=mij,ok=1;
        else if(q[mij<x])
            i=mij+1;
    }

    if(ok)
        return j;
    return 0;
}

void dinamic()
{
    int i,j;
//  best[n]=1;
//  poz[n]=-1;
//  maxi=1; p=n;
//  for(i=n-1;i>=1;--i)
//   {
//   best[i]=1;
//   poz[i]=-1;
//   for(j=i+1;j<=n;++j)
//       if(a[i]<a[j] && best[i]<best[j]+1)
//         {
//         best[i]=best[j]+1;
//         poz[i]=j;
//         if(best[i]>maxi) maxi=best[i],p=i;
//         }
//   }
    for(i=1; i<=n; i++)
    {
        if(j=bs(q,a[i]))
            q[j]=a[i],poz[++k]=j;
        else
            q[++sf]=a[i],poz[++k]=sf;
    }
}
void constr()
{
    int i;
    //i=p;
    //while(i!=-1)
    // {
    // o<<a[i]<<' ';
    //i=poz[i];
    // }
    int aux=sf;
    for(i=k; i>=1; i--)
    {
        if(poz[i]==aux)
            best[p++]=a[i],aux--;
    }

}
int main()
{
    read();
    dinamic();
    o<<sf<<endl;
    constr();
    for(int i=p-1; i>=0; i--)
        o<<best[i]<<' ';
    return 0;
}