Cod sursa(job #2174849)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 16 martie 2018 13:48:40
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

int n,k,a[100005],st[100005];
int poz[100005],pozitie;

void cautabin(int left,int right,int x)
{
    int mid;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(st[mid]==x)
            pozitie = mid;
        if(st[mid]<=x)
        {
            right = mid-1;
        }else
            left = mid+1;
    }
}

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

    st[1]=a[1];
    k=1;
    poz[1]=1;

    for(int i=2;i<=n;++i)
    {
        if(a[i]<st[1])
        {
            st[1]=a[i];
            poz[i]=1;
        } else if(a[i]>st[k])
        {
            k++;
            poz[i]=k;
            st[k]=a[i];
        }else{
            pozitie = INT_MAX;
            cautabin(1,k,a[i]);
            st[pozitie]=a[i];
            poz[i]=pozitie;
        }
    }
g<<k<<'\n';
vector <int> V;
for(int i=n;i>=1&&k;--i)
    if(poz[i]==k)
    {
        V.push_back(a[i]);
        k--;
    }
for(int i=V.size()-1;i>=0;--i)
    g<<V[i]<<" ";
return 0;

}