Pagini recente » Cod sursa (job #764322) | Cod sursa (job #1126813) | Cod sursa (job #1422035) | Cod sursa (job #59337) | Cod sursa (job #522700)
Cod sursa(job #522700)
/*
* File: main.cpp
* Author: salexandru
*
* Created on January 15, 2011, 8:58 PM
*/
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011
using namespace std;
/*
*
*/
int f[N_MAX];
vector<int> TopMax, v, l;
inline void output( ostream& out, int& x )
{
if( -1 != x )
{
output( out, f[x] );
out<<v[x]<<' ';
}
}
int main(int argc, char** argv)
{
int N, i, left, right, middle, indexWhere, indexMax=0;
ifstream in( "scmax.in" );
in>>N;
copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
TopMax.push_back(0);
l.push_back(1);
f[0]=-1;
for( i=1; i < N; ++i )
{
indexWhere=-1;
left=0; right=TopMax.size()-1;
while( left <= right )
{
middle=(left+right)/2;
if( v[TopMax[middle]] < v[i] )
{
if( -1 == indexWhere || l[indexWhere] < l[middle] )
indexWhere=middle;
right=middle-1;
}
else left=middle+1;
}
if( -1 == indexWhere )
{
f[i]=-1;
TopMax.push_back(i);
l.push_back(1);
}
else {
f[i]=TopMax[indexWhere];
TopMax[indexWhere]=i;
l[indexWhere]+=1;
if( l[indexWhere] > l[indexMax] )
indexMax=indexWhere;
}
}
ofstream out( "scmax.out" );
out<<l[indexMax]<<'\n';
output( out, TopMax[indexMax] );
out<<'\n';
return 0;
}