Cod sursa(job #649027)

Utilizator maritimCristian Lambru maritim Data 15 decembrie 2011 01:15:29
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include<stdio.h>
#include<fstream>
using namespace std;

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

typedef struct
{
	int x,y,z;
} xy;

#define MaxN 100100

int N,T[MaxN];
xy A[MaxN],B[MaxN],Trie[MaxN];

void citire(void)
{
	f >> N;
	for(int i=1;i<=N;i++)
		f >> A[i].x;
}

int cmp(xy a,xy b)
{
	return a.x < b.x;
}

void Normalizare(void)
{
	for(int i=1;i<=N;i++)
		B[i].x = A[i].x, B[i].y = i;
	
	sort(B+1,B+N+1,cmp);
	
	for(int i=1;i<=N;i++)
	{
		B[i].z = B[i].x != B[i-1].x ?  B[i-1].z+1 : B[i-1].z;
		A[B[i].y].y = B[i].z;
	}
}

void UpdateAIB(int a,int val,int poz)
{
	for(;a <= N;)
	{
		val > Trie[a].x ? Trie[a].x = val,Trie[a].y = poz : 0;
		a += a & -a;
	}
}

void MaxAIB(int a,int &val,int &poz)
{	
	for(;a;)
	{
		val < Trie[a].x ? val = Trie[a].x, poz = Trie[a].y : 0;
		a -= a & -a;
	}
}

void Scmax(void)
{
	for(int i=1;i<=N;i++)
	{
		Trie[A[i].y].x = 0;
		MaxAIB(A[i].y-1,Trie[A[i].y].x,T[i]);
		Trie[A[i].y].x ++;
		Trie[A[i].y].y = i;
		UpdateAIB(A[i].y,Trie[A[i].y].x,i);
	}
}

void Afis(int a)
{
	if(a)
	{
		Afis(T[a]);
		g << A[a].x << " ";
	}
}

int main()
{
	citire();
	Normalizare();
	Scmax();
	
	int MAX,poz;
	
	MaxAIB(N,MAX,poz);
	
	g << MAX << "\n";
	Afis(poz);
	
	return 0;
}