Cod sursa(job #2241151)

Utilizator niculaandreiNicula Andrei Bogdan niculaandrei Data 15 septembrie 2018 00:52:38
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, v[100005], q[100005], p[100005], pre[100005], i, j, poz, k, mx;
int caut(int l, int r, int x)
{
    int m, best = 0;
    while (l <= r)
    {
        m = (l + r) / 2;
        if (v[q[m]] >= x)
        {
            best = m;
            r = m - 1;
        }
        else l = m + 1;
    }
    return best;
}
void afis(int i)
{
    if (pre[i] > 0) afis(pre[i]);
    fout << v[i] << " ";
}
int main()
{
    fin >> n;
    for (i = 1; i <= n; i++)
    {
        fin >> v[i];
        if (v[i] > v[q[k]]) poz = ++k;
        else poz = caut(1, k, v[i]);
        q[poz] = i;
        pre[i] = q[poz - 1];
        p[i] = poz;
        if (poz == k) mx = i;
    }
    fout << k << "\n";
    afis(mx);
    return 0;
}