Cod sursa(job #1226939)

Utilizator IulianBoboUAIC Boboc Iulian IulianBobo Data 9 septembrie 2014 09:02:17
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

int best[100001],pre[100001],highestL[100001],maxL=1;

int search(int *a,int x)
{
	int left = 0,right = maxL,mid;
	mid = (left+right)/2;
	while(left <= right)
	{
		if(a[ highestL[mid] ] < x && x <= a[ highestL[mid+1] ]) return mid;
		else if(a[ highestL[mid + 1] ] < x)
		{
			left = mid+1;
			mid = (left+right)/2;
		}
			else
			{
				right = mid - 1;
				mid = (left+right)/2;
			}
			
	}
	return right;
}

void compute(int *a,int n,int &maxim,int &ind)
{
	int pos,i;
	highestL[1] = best[1] = 1;
	highestL[0] = 0;
	for(i = 2;i <= n;++i)
	{
		pos = search(a,a[i]);
		best[i] = pos+1;
		highestL[ pos + 1 ] = i;
		pre[i] = highestL[pos];
		if(maxL < pos + 1) maxL = pos + 1;
	}
	for(i = 1;i <= n;++i)
	{
		if(best[i]>maxim)
		{			
			maxim=best[i];
			ind=i;
		}
	}
}
	
void scrie(int *a,int ind)
{
	if(a[ind]>0) 
	{
		scrie(a,pre[ind]);
		g<<a[ind]<<" ";
	}
}

void readData(int* a,int &n)
{
	f>>n;
	for(int i=1;i<=n;++i) f>>a[i];
	f.close();
}

void printData(int *a,int maxim,int ind)
{
	g<<maxim<<"\n";
	scrie(a,ind);
	g.close();
}


	
int main()
{
	int v[100001],n,maxim=0,ind;
	readData(v,n);
	compute(v,n,maxim,ind);
	printData(v,maxim,ind);
	return 0;
}