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