Cod sursa(job #1477497)

Utilizator ballsMingiuci balls Data 26 august 2015 14:12:21
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <unordered_map>

using namespace std;
typedef int var;


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

int V[200000], SCM[200000], Parent[200000];

struct ArbInt {
    #define zeros(a) (a&-a)
    unordered_map<int, int> T;

    void insert(int poz, int val) {
        for(;poz>0; poz += zeros(poz))
            if(SCM[T[poz]] < SCM[val])
                T[poz] = val;
    }

    int query(int poz) {
        int r = 0;
        for(;poz;poz-=zeros(poz))
            if(SCM[r] < SCM[T[poz]])
                r = T[poz];
        return r;
    }
};
ArbInt T;

void afis(int node) {
    if(Parent[node]) afis(Parent[node]);
    fout<<V[node]<<" ";
}

int main() {

    int n, val;
    fin>>n;

    int start = 0;
    for(int i=1; i<=n; i++) {
        fin>>V[i];

        Parent[i] = T.query(V[i] - 1);
        SCM[i] = 1 + SCM[Parent[i]];

        if(SCM[start] < SCM[i])
            start = i;

        T.insert(V[i], i);
    }

    fout<<SCM[start]<<'\n';
    afis(start);

    return 0;
}