Cod sursa(job #2153435)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 6 martie 2018 10:44:18
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long N, MAX, a[100003], p[100003], st[100003];
int k;
int poz;

int cautbin(long long x);

int main() {

    f >> N;
    for (int i = 1; i <= N; ++i) {
        f >> a[i];
        int pozt = cautbin(a[i]);
        st[(k = pozt)] = a[i];
        p[i] = k;
        if(MAX < p[i]) MAX = p[i], poz = i;
    }
    g << MAX << "\n";
    k = 0;
    st[++k] = a[poz];
    MAX--;
    for(int i = poz - 1; i >= 1; i--) {
        if(p[i] == MAX && a[i] < st[k]) {
            st[++k] = a[i];
            MAX--;
        }
    }
    for (int i = k; i >= 1; i--) {
        g << st[i] << " ";
    }
    g << "\n";
    return 0;
}

int cautbin(long long x) {
    int s = 1, d = k;
    while(s <= d) {
        int mij = (s + d) / 2;
        if(st[mij] == x)
            break;
        else if (st[mij] < x)
            s = mij + 1;
        else
            d = mij - 1;
    }
    return s;
}