Cod sursa(job #1246316)

Utilizator sefubanilorSefu Banilor sefubanilor Data 20 octombrie 2014 22:11:33
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>
#include <algorithm>
#include <iostream>

using namespace std;

const char InFile[]="scmax.in";
const char OutFile[]="scmax.out";
const int MaxN=100111;

ifstream fin(InFile);
ofstream fout(OutFile);

int N,Prev[MaxN],best[MaxN],V[MaxN],best_ind[MaxN],bestSol,bestInd;

void afis(int ind)
{
    if(!ind)
    {
        return;
    }
    afis(Prev[ind]);
    fout<<V[ind]<<" ";
}

inline int bs(const int &ind)
{
    int first=0;
    int step=1<<16;
    for(;step;step>>=1)
    {
        int index=first+step;
        if(index<=best_ind[0])
        {
            if(V[ind]>V[best_ind[index]])
            {
                first=index;
            }
        }
    }
    return first;
}

int main()
{
    fin>>N;
    for(int i=1;i<=N;++i)
    {
        fin>>V[i];
    }
    fin.close();

    for(int i=1;i<=N;++i)
    {
        best[i]=bs(i);
        if(best[i]==0)
        {
            Prev[i]=0;
        }
        else
        {
            Prev[i]=best_ind[best[i]];
        }
        ++best[i];
        if(best_ind[0]<best[i])
        {
            best_ind[++best_ind[0]]=i;
        }
        else if(V[best_ind[best[i]]]>V[i])
        {
            best_ind[best[i]]=i;
        }

        if(best[i]>bestSol)
        {
            bestSol=best[i];
            bestInd=i;
        }
    }

    fout<<bestSol<<"\n";
    afis(bestInd);

    fout.close();
    return 0;
}