Cod sursa(job #2056370)

Utilizator netfreeAndrei Muntean netfree Data 4 noiembrie 2017 11:20:07
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

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

const int inf = INT_MAX / 3;
const int N_MAX = 100000 + 5;

int n, dpn, x[N_MAX], dp[N_MAX];

int binar(int val){
    int i, step;
    for (step = 1; step < dpn; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < dpn && dp[i + step] <= val)
           i += step;
    return i;
}



int main()
{
    dp[0] = -inf; dpn = 0;

    fin >> n;

    for(int i = 1, nr, pos; i<=n; ++i){
        fin >> nr;
        if(nr > dp[dpn])
            dp[++dpn] = nr;
        else{
            pos = binar(nr);
            if(dp[pos] != nr)
                dp[pos + 1] = nr;
        }
    }

    fout << dpn << "\n";

    for(int i = 1; i<=dpn; ++i)
        fout << dp[i] << " ";

    return 0;
}