Cod sursa(job #1375057)

Utilizator StexanIarca Stefan Stexan Data 5 martie 2015 11:54:40
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#define NMAX 100005

using namespace std;

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


int N,Value[NMAX],DP[NMAX],Next[NMAX];
int Father;

void Read(){
    f>>N;
    for (int i = 1; i <= N; ++i) {
        f>>Value[i];
    }
}

void Solve(){
    for (int i = N; i >= 1; i--) {
        DP[i] = 0;
        for (int j = i+1; j <= N; j++) {
            if(Value[i] < Value[j] && DP[i] < DP[j]){
                DP[i] = DP[j];
                Next[i] = j;
            }
        }
        DP[i]++;
    }
}

void Write(){
    int max = 0;
    for (int i = 0; i <= N; ++i) {
        if (max < DP[i]) {
            max = DP[i];
            Father = i;
        }
    }
    g<<max<<"\n";
    while (Father) {
        g<<Value[Father]<<" ";
        Father = Next[Father];
    }
}

int main() {

    Read();
    Solve();
    Write();
    
    return 0;
}