Cod sursa(job #2871798)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 15 martie 2022 19:27:42
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <bits/stdc++.h>
using namespace std;

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

int arbint[400005];
int lis[100005],v[100005];
pair < int , int > p[100005];
int n;
int ind,val,end_ind,max_el=2000000001;
stack < int > st;

bool comp(pair < int , int > a, pair < int , int > b) {
    if (a.first!=b.first) return a.first<b.first;
    return a.second>b.second;
}

void read() {
    f >> n;
    for (int i=1;i<=n;i++) {
        f >> v[i];
        p[i].first = v[i];
        p[i].second = i;
    }
    sort(p+1,p+n+1,comp);
}

void update(int pos, int st, int dr) {
    if (ind<st || dr<ind) return;
    if (st==dr) {
        arbint[pos] = val;
        lis[st] = val;
        return;
    }
    int mij = (st+dr)/2;
    update(2*pos,st,mij);
    update(2*pos+1,mij+1,dr);
    arbint[pos]=max(arbint[2*pos],arbint[2*pos+1]);
}

int find_val(int pos, int st, int dr) {
    if (dr<=end_ind) return arbint[pos];
    if (end_ind<st) return 0;
    int mij = (st+dr)/2;
    return max(find_val(2*pos,st,mij),find_val(2*pos+1,mij+1,dr));
}

void solve() {
    for (int i=1;i<=n;i++) {
        end_ind = p[i].second;
        val = find_val(1,1,n)+1;
        ind = p[i].second;
        update(1,1,n);
    }
    g << arbint[1] << '\n';
    for (int i=n;i>=1;i--) {
        if (lis[i]==arbint[1] && v[i]<max_el) {
            st.push(v[i]);
            max_el = v[i];
            arbint[1]--;
        }
    }
    while (st.empty()==0) {
        g << st.top() << " ";
        st.pop();
    }
}

int main()
{
    read();
    solve();
    return 0;
}