Cod sursa(job #2174886)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 16 martie 2018 14:01:09
Problema Subsir crescator maximal Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 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;
            right=mid-1;
        }else
            left=mid+1;
    }
}

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

    k=1;
    st[k]=a[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 = k;
            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;

}