Cod sursa(job #2468969)

Utilizator Codrut112Codrut Copas Codrut112 Data 6 octombrie 2019 12:45:33
Problema Subsir crescator maximal Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <fstream>
# define Nmax 100005
using namespace std;
int n,v[Nmax],a[Nmax],i,w[Nmax],x,lung,lungmax,j,poz,q[Nmax],s,d;
long long  mn;
ifstream f("scmax.in");
ofstream g("scmax.out");
int caut(int x)
{
    int s=1,d=lung,poz=-1;
    bool gasit=true;
    while(s<=d)
    {

        int mid=(s+d)/2;
        if(x==a[mid]) gasit=false;
        if(x<a[mid])
        {
            d=mid-1;
            poz=mid;
        }
        if(x>=a[mid])s=mid+1;

    }
    if(gasit==false)poz=0;
    return poz;

}
int main()
{
    f>>n;
    for(i=1; i<=n; i++) f>>v[i];
    lung=1;
    a[1]=v[1];
    for(i=2; i<=n; i++)
    {

        poz=caut(v[i]);
        if(poz==-1)
        {
            lung++;q[i]=lung;
            a[lung]=v[i];
        }
        else if(poz!=0)
        {a[poz]=v[i];
        q[i]=poz;

        }


    if(lung>lungmax)
        {
            lungmax=lung;
            for(j=1; j<=lungmax; j++)w[j]=a[j];
        }
    }
    g<<lungmax<<'\n';lung=lungmax;
    s=1;
 for(i=1;i<=lung;i++){mn= 2000000001;
  d=s;
 while(q[d]<=i){d++;if(v[d]<mn and q[d]==i) {mn=v[d];s=d;}   }
   w[i]=mn;

 }    for(i=1;i<=lung;i++) g<<w[i]<<" ";


}