Cod sursa(job #2095829)

Utilizator zanugMatyas Gergely zanug Data 28 decembrie 2017 12:23:45
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define pb push_back

using namespace std;

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

const int N = 1e5 + 10;

int n;
int v[N];
vector < int > x;
int el[N];
stack < int > st;

int main()
{
    fin >> n;
    for(int i = 0; i < n; ++i)
        fin >> v[i];
//
    x.pb(0);
    for(int i = 1; i < n; ++i)
    {
        if(v[i] > v[x.back()])
        {
            el[i] = x.back();
            x.pb(i);
        }
        else
        {
            ///binary search
            int l = 0, r = x.size() - 1;
            while(l < r)
            {
                int m = (l + r) / 2;

                if(v[x[m]] > v[i])
                    r = m;
                else
                    l = m+1;
            }

            if(v[x[l]] > v[i] && (l == 0 || v[x[l-1]] < v[i]))
            {
                x[l] = i;
                if(l > 0)
                    el[i] = x[l-1];
            }
        }

    }
//
    fout << x.size() << "\n";

    int i = x.back();
    while(st.size() < x.size())
    {
        st.push(v[i]);
        i = el[i];
    }
    while(st.size())
    {
        fout << st.top() << " ";
        st.pop();
    }

    fout << "\n";

    return 0;
}