Cod sursa(job #736222)

Utilizator BitOneSAlexandru BitOne Data 18 aprilie 2012 10:17:22
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011

using namespace std;

int f[N_MAX];
vector< int > v, TopMax;

inline void Output(const int& x, ostream& out)
{
    if(-1 == f[x])
       out<<v[x]<<' ';
    else {
            Output(f[x], out);
            out<<v[x]<<' ';
         }
}
int main()
{
    int N, i, x, left, middle, right;
    ifstream in("scmax.in");
    ofstream out("scmax.out");

    in>>N;
    copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v));
    fill(f, f+N+1, -1);
    TopMax.push_back(0);
    for(i=1; i < N; ++i)
    {
        x=TopMax.back();
        if(v[x] == v[i])
            continue;
        if(v[x] < v[i])
        {
            f[i]=x;
            TopMax.push_back(i);
            continue;
        }
        left=0; right=TopMax.size();
        while(left < right)
        {
            middle=(left+right)>>1;
            if(v[TopMax[middle]] == v[i])
            {
                left=middle;
                break;
            }
            if(v[i] <= v[TopMax[middle]])
                right=middle;
            else left=middle+1;
        }
        if(v[i] < v[TopMax[left]])
        {
            if(left)
                f[i]=TopMax[left-1];
            TopMax[left]=i;
        }
    }
    out<<TopMax.size()<<'\n';
    Output(TopMax.back(), out);

    return EXIT_SUCCESS;
}