Cod sursa(job #15024)

Utilizator raula_sanChis Raoul raula_san Data 10 februarie 2007 15:54:53
Problema Subsir 2 Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include<cstdio>
#define dim 5005

int N, M;
int P[dim];
long A[dim], B[dim];

void read();
void dynamics();
void write();

int cbin( long );

int main()
{
	read();
	dynamics();
	write();
	
	return 0;
}

void read()
{
	freopen("subsir2.in", "r", stdin);
	scanf("%d", &N);
	
	int i;
	for(i=1; i<=N; ++i) scanf("%ld", A+i);
	
	fclose(stdin);
}

void dynamics()
{
	int i, pos;
	for(i=1; i<=N; ++i)
	{
		if(A[i] >= B[M])
		{
			++ M;
			B[M] = A[i];
			P[i] = M;
		}
		else
		{
			pos = cbin(A[i]);
			B[pos] = A[i];
			P[i] = pos;
		}
	}
}

int cbin( long value )
{
	int st=1, dr=M, m;
	
	while( st <= dr )
	{
		m = (st+dr)>>1;
		
		if(m == 1 && value < B[m]) return m;
		else if(m < M)
			if(B[m] <= value && B[m+1] > value) return m+1;
		
		if(value < B[m])  dr=m-1;
		if(value >= B[m]) st=m+1;
	}
	return 0;
}

void write()
{
	freopen("subsir2.out", "w", stdout);
	printf("%d\n", M);
	
	int i, k=0;
	for(i=N; i; --i)
	{
		if(P[i] == M)
		{
			B[++k] = i;
			-- M;
		}
		if(!M) break;
	}
	for(i=k; i; --i) printf("%ld ", B[i]);
	fclose(stdout);
}