Cod sursa(job #350721)
/*
* File: main.cpp
* Author: speedemon
*
* Created on September 19, 2009, 6:06 PM
*/
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define InFile "roci.in"
#define OutFile "roci.out"
/*
*
*/
using namespace std;
ifstream in;
ofstream out;
vector<int> TopPile;
int main()
{int N,i,pile,st,dr,nr_piles=1,m,maxL=-1,poz,pozs;
int *v,*p,*Length;
in.open(InFile);
in>>N;
v=(int*)calloc( N, sizeof(int) );
p=(int*)calloc( N, sizeof(int) );
Length=(int*)calloc( N, sizeof(int) );
for( i=0; i < N; ++i )
{p[i]=N; Length[i]=1;
in>>v[i];
}
TopPile.push_back(0);
for( i=1; i < N; ++i )
{pile=-1;
st=0; dr=TopPile.size()-1;
while( st < dr )
{
m=st+(dr-st)/2;
if( v[TopPile[m]] < v[i] )
{
if( -1 == pile || Length[m] > Length[pile] )
{
pile=m;
}
if( v[TopPile[m+1]] >= v[i] ) dr=m-1;
else st=m+1;
}
else dr=m-1;
}
if( -1 == pile )
{
TopPile.push_back(i);
if( maxL < 1 ) maxL=1,poz=i;
}
else {
p[i]=TopPile[pile];
TopPile[pile]=i;
++Length[pile];
if( maxL < Length[pile] ) maxL=Length[pile],poz=i;
}
}
out.open(OutFile);
out<<maxL<<'\n';
if( 1 == maxL ) out<<v[poz];
else {vector<int> b;
while( N != poz )
{pozs=poz;
b.push_back(v[poz]);
poz=p[poz]; p[pozs]=N;
}
copy( b.rbegin(), b.rend(), ostream_iterator<int>(out," ") );
}
free(v); free(p); free(Length);
return EXIT_SUCCESS;
}