Cod sursa(job #1134438)

Utilizator andreiagAndrei Galusca andreiag Data 6 martie 2014 15:41:34
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const int Nmax = 100005;

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

int A[Nmax];    // original sequence
int B[Nmax];    // longest subsequence
int p[Nmax];    // previous

void print(int x)
{
    if (p[x] > -1) print(p[x]);
    g << A[x] << ' ';
}

int main()
{
    
    int N;
    f >> N;
    for (int i = 0; i < N; i++) {
        f >> A[i];
        p[i] = -1;
    }

    B[0] = 0;
    int pos = 0, k = pos;

    for (int i = 1; i < N; i++) {
        if (A[i] > A[B[pos]]) {
            B[pos+1] = i;
            p[i] = B[pos];
            pos++;
        } else {
            k = pos;
            while (k > 0 && !(A[i] > A[B[k-1]])) k--;
            p[i] = k > 0 ? B[k-1] : -1;
            B[k] = i;
        }
    }

    g << pos+1 << '\n';
    print(B[pos]);
    g << endl;

    return 0;
}