Cod sursa(job #2225048)

Utilizator vladm98Munteanu Vlad vladm98 Data 25 iulie 2018 19:34:43
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>

using namespace std;

int maximum [100005];
int before [100005];
int v[100005];
int dp[100005];
int reversed[100005];
map <int, int> normalization;

int mostUnsiginficantBit (int x) {
    return x & (-x);
}

void updateValue (int n, int position, int value) {
    while (position <= n) {
        if (dp[maximum[position]] < dp[value]) {
            maximum[position] = value;
        }
        position += mostUnsiginficantBit(position);
    }
}

int getMaximum (int position) {
    int current = 0;
    while (position) {
        if (dp[current] < dp[maximum[position]]) {
            current = maximum[position];
        }
        position -= mostUnsiginficantBit(position);
    }
    return current;
}

int main() {
//    ifstream fin ("input");
//    ofstream fout ("output");
    ifstream fin ("scmax.in");
    ofstream fout ("scmax.out");
    int n;
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        normalization[v[i]] += 1;
    }
    int currentValue = 1;
    for (auto &x : normalization) {
        x.second = currentValue;
        reversed[currentValue] = x.first;
        currentValue += 1;
    }
    int theBest = 0;
    for (int i = 1; i <= n; ++i) {
        v[i] = normalization[v[i]];
        int bestPosition = getMaximum(v[i] - 1);
        dp[i] = dp[bestPosition] + 1;
        before[i] = bestPosition;
        if (dp[theBest] <= dp[i]) {
            theBest = i;
        }
        updateValue(n, v[i], i);
    }
    fout << dp[theBest] << '\n';
    vector <int> answer;
    while (theBest) {
        answer.push_back(reversed[v[theBest]]);
        theBest = before[theBest];
    }
    reverse (answer.begin(), answer.end());
    for (auto x : answer) {
        fout << x << ' ';
    }
    return 0;
}