Cod sursa(job #1210676)

Utilizator pop_bogdanBogdan Pop pop_bogdan Data 20 iulie 2014 19:31:18
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <fstream>
using namespace std;

ifstream is("scmax.in");
ofstream os("scmax.out");

#define DIM 100003

int N, x[DIM];
int lenS, aux;
pair <int,int> S[DIM];
int parent[DIM];

void Input();
void Solve();
int BinarySearch(int x);
void Output(int i);
void Debug();

int main()
{
    Input();
    Solve();

    is.close();
    os.close();
}

void Input()
{
    is >> N;
    for ( int i = 1; i <= N; ++i )
        is >> x[i];
    lenS = 1; S[lenS].first = x[1]; S[lenS].second = 1;
}

void Solve()
{
    for ( int i = 2; i <= N; ++i )
    {
        if ( x[i] > S[lenS].first )
        {
            S[++lenS].first = x[i];
            S[lenS].second = i;
            parent[i] = S[lenS-1].second;
        }
        else
        {
            aux = BinarySearch(x[i]);
            parent[i] = S[aux-1].second;
            S[aux].first = x[i];
            S[aux].second = i;
        }
    }
    os << lenS << '\n';
    Output(S[lenS].second);

}

int BinarySearch(int x)
{
    int lo(1), hi(lenS), mid;
    while ( lo < hi )
    {
        mid = (lo+hi)/2;
        if ( S[mid].first >= x )
            hi = mid;
        else
            lo = mid+1;
    }
    return hi;
}

void Debug()
{
    for ( int i = 1; i <= lenS; ++i )
        os << S[i].first << " ";
    os << '\n';
}

void Output(int i)
{
    if ( i >= 1 )    Output(parent[i]);
    if ( i >= 1)
    os << x[i] << " ";
}