Cod sursa(job #2484736)

Utilizator 0738076326Simon Wil 0738076326 Data 31 octombrie 2019 15:21:20
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.03 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5 + 5;
pair <int, int> v[NMAX];
int n,arb[4 * NMAX],dp[NMAX],value,a[NMAX], poz[4 * NMAX], p;

bool cmp(pair <int, int> X, pair <int, int> Y){
    if(X.first != Y.first)
        return X.first < Y.first;
    return X.second > Y.second;
}

void query(int node, int lo, int hi, int x){
    if(lo > x) return;
    if(lo > hi) return;
    if(hi <= x){
        //value = max(value, arb[node]);
        if(value < arb[node]){
            value = arb[node];
            p = poz[node];
        }
        return;
    }
    int mid = (lo + hi) / 2;
    query(2 * node, lo, mid, x);
    if(mid < x)
        query(2 * node + 1, mid + 1, hi, x);
}

void update(int node, int lo, int hi, int x, int y){
    if(lo == hi){
        arb[node] = y;
        poz[node] = lo;
        return;
    }
    //if(lo > x || hi < x) return;
    int mid = (lo + hi) / 2;
    if(x <= mid)
        update(2 * node, lo, mid, x, y);
    else
        update(2 * node + 1, mid + 1, hi, x, y);
    if(arb[2 * node] >= arb[2 * node + 1]){
        arb[node] = arb[2 * node];
        poz[node] = poz[2 * node];
    }else{
        arb[node] = arb[2 * node + 1];
        poz[node] = poz[2 * node + 1];
    }
}

void see(int x){
    if(dp[x] == 0){
        g << a[x] << " ";
        return;
    }
    see(dp[x]);
    g << a[x] << " ";
}

int main(){
    int i,maxim = 0, pos;
    f >> n;
    for(i = 1 ; i <= n ; i++){
        f >> v[i].first;
        a[i] = v[i].first;
        v[i].second = i;
    }

    sort(v + 1, v + n + 1, cmp);

    for(i = 1 ; i <= n ; i++){
        value = 0;
        p = 0;
        query(1,1,n,v[i].second - 1);
        value++;
        dp[v[i].second] = p;
        update(1,1,n,v[i].second, value);
        //g << value << " ";
        if(value > maxim){
            maxim = value;
            pos = v[i].second;
        }
    }

    g << maxim << "\n";
    see(pos);

    return 0;
}