Cod sursa(job #2074984)

Utilizator LucaSeriSeritan Luca LucaSeri Data 25 noiembrie 2017 10:31:45
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <stack>
using namespace std;
const int MAXN = 100100;
const int inf = 1e9;

int dp[MAXN];
int last[MAXN];
int v[MAXN];
int n;

void binara(int ind){
    int best = 0;
    for(int step = 30; step >= 0; --step){
        if((best + (1<<step) <= n) && dp[(best+(1<<step))] != inf && v[dp[best+(1<<step)]] < v[ind]){
            best += (1<<step);
        }
    }

    dp[best + 1] = ind;
    last[ind] = dp[best];
}

int main(){

    ifstream cin("scmax.in");
    ofstream cout("scmax.out");

    cin >> n;

    for(int i = 1; i <= n; ++i){
        dp[i] = inf;
    }

    for(int i = 1; i <= n; ++i){
        cin >> v[i];
        binara(i);
    }

    stack<int> ans;
    for(int i = n; i >= 1; --i){
        if(dp[i] != inf){
            cout << i << '\n';
            int curent = dp[i];
            while(curent){
                ans.push(curent);
                curent = last[curent];
            }

            while(ans.size()){
                cout << v[ans.top()] << ' ';
                ans.pop();
            }
            break;
        }
    }
    return 0;
}