Cod sursa(job #2115598)

Utilizator DawlauAndrei Blahovici Dawlau Data 26 ianuarie 2018 22:01:51
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include<fstream>
#include<climits>
#include<stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMAX = 100005, INF = INT_MAX;

int LISLength[NMAX], LIS[NMAX], numbers[NMAX];
int numbersCount, length = 1;

inline void read_data(){

    fin >> numbersCount;

    int index;
    for(index = 1; index <= numbersCount; ++index)
        fin >> numbers[index];
}

inline int getBestPos(int value, int indexInLIS){

    int left = 1, right = length, mid;

    while(right - left){

        mid = (left + right) >> 1;
        if(LISLength[mid] < value)
            left = mid + 1;
        else
            right = mid;
    }

    LISLength[left] = value;

    if(left == length){

        ++length;
        LISLength[length] = INF;
    }

    return left;
}

inline void getLIS(){

    LISLength[1] = INF;

    int index;
    for(index = 1; index <= numbersCount; ++index)
        LIS[index] = getBestPos(numbers[index], index);

    --length;
}

inline void printLIS(){

    fout << length << '\n';

    stack<int> Stack;

    int index;
    for(index = numbersCount; index >= 1; --index)
        if(LIS[index] == length){

            Stack.push(numbers[index]);
            --length;
        }

    while(!Stack.empty()){

        fout << Stack.top() << ' ';
        Stack.pop();
    }
}
int main(){

    read_data();
    getLIS();
    printLIS();
}