Cod sursa(job #2648645)

Utilizator Casian_doispeChiriac Casian Casian_doispe Data 12 septembrie 2020 08:28:40
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
#include <fstream>
#include <deque>
#include <vector>
#include <bitset>
#include <queue>
#include <unordered_set>
#include <algorithm>
#include <cmath>

#define MOD 666013

using namespace std ;

ifstream cin ("scmax.in") ;
ofstream cout ("scmax.out") ;

/// folosim un vector auxiliar pentru a cauta valorile de sfarsit unde se incadreaza un nou element adaugat
/// in vectorul final, acesta este sortat crescator, iar daca nu se gaseste nici un element mai mic decat cel
/// curent atunci cel curent se adauga la final

vector<int> v_final, v ;

int poz_final[100009] ;

void recur(int cautatul, int n)
{
    if(cautatul < 0)return ;

    for(int f = n ; f >= 0 ; f --)
    {

        if(poz_final[f] == cautatul)
        {

            recur(cautatul - 1, f - 1) ;

            cout << v[f] << " " ;

            return ;

        }

    }

}

namespace FastRead
{

    FILE *ptr = fopen("parsare.in", "r") ;

    int DIM = 5000 ;

    char buff[10009] ;

    int ipos = 0, ilen = 0 ;

    char NextCharacter()
    {

        if(ipos == ilen)
        {

            ilen = fread(buff, 1, DIM, ptr) ;

            ipos = 0 ;

            if(!ilen)return EOF ;

        }

        return buff[ipos ++] ;

    }

    bool Read(int &x)
    {

        int semn = 1 ;

        char ch ;

        while(!isdigit(ch = NextCharacter()))
            if(ch == '-')semn *= -1 ;
                else if(ch == EOF)return 0 ;

        x = ch - '0' ;

        while(isdigit(ch = NextCharacter())){x = x * 10 + ch - '0'; if(ch == EOF)return 0 ;};

        x *= semn ;

        return 1 ;

    }

}


int main()
{
    int n ;

    FastRead::Read(n) ;

    for(int f = 1, x ; f <= n ; f ++)
    {

        FastRead::Read(x) ;

        v.push_back(x) ;
    }

    for(int f = 0 ; f < n ; f ++)
    {
        int poz = v_final.size() ;

        if(v_final.empty() || v[f] > v_final.back())v_final.push_back(v[f]) ;
            else poz = lower_bound(v_final.begin(), v_final.end(), v[f]) - v_final.begin() ;

        v_final[poz] = v[f] ;

        poz_final[f] = poz ;

    }

    cout << v_final.size() << endl ;

    recur(v_final.size() - 1, n - 1) ;

    return 0;
}