Cod sursa(job #2174128)

Utilizator mihaialex14Dima Mihai mihaialex14 Data 16 martie 2018 10:52:53
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>
#define N 100005
using namespace std;

ifstream fin("drept.in");
ofstream fout("drept.out");


int n,a[N];
int tail[N];

void Read(){
    int i;

    fin>>n;
    for(i=0; i<n; i++) fin>>a[i];

    fin.close();
}

///Cautare binara
int CeilIndex(int v[], int l, int r, int key){
    while(r-1 > 1)
    {
        int m= l+ (r-1)/2;
        if(a[m] >= a[key] ) r=m;
        else l=m;
    }

    return r;
}

int LIS(){
    int i, length=1;
    tail[0]=a[0];
    for(i=1; i<n; i++)
        if(a[i] < tail[0])
            ///noua cea mai mica valoare
            tail[0]=a[i];
        else if(a[i] > tail[length-1])
                ///extindem subsirul cel mai lung
                tail[length++]=a[i];
        else
            /// v[i] will become end candidate of an existing subsequence or
            /// Throw away larger elements in all LIS, to make room for upcoming grater elements than v[i]
            /// (and also, v[i] would have already appeared in one of LIS, identify the location and replace it)
            tail[CeilIndex (tail, -1, length-1, a[i])]= a[i];

    return length;
}

int main()
{
    Read();

    int lg=LIS();
    fout<<lg<<'\n';
    for(int i=0; i<lg; i++) fout<<tail[i]<<' ';
    return 0;
}