Cod sursa(job #184251)

Utilizator tm_raduToma Radu tm_radu Data 23 aprilie 2008 12:42:46
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <stdio.h>
#define NM 100001

int n, a[NM], l[NM], t[NM], s[NM];
int i, j, k, lmax, imax;

void Cbin(int st, int dr);
void Drum(int nod);

int main()
{
    freopen("scmax.in", "r", stdin);
	freopen("scmax.out", "w", stdout);
	scanf("%d", &n);
	for ( i = 1; i <= n; i++ )
		scanf("%d", &a[i]);

	for ( i = 1; i <= n; i++ )
	{
		if ( a[i] > a[s[k]] ) k++, s[k] = i, j = k;
	    else                  Cbin(1, k), s[j] = i;
		l[i] = j, t[i] = s[j-1];
		if ( l[i] > lmax ) lmax = l[i], imax = i;
	}
	printf("%d\n", lmax);
	Drum(imax); printf("\n");
	
	return 0;
}

void Cbin(int st, int dr)
{
	while ( st != dr )
	{
		int mij = (st+dr)>>1;
		if ( a[i] <= a[s[mij]] ) dr = mij;
		else                    st = mij+1;
	}
	j = st;
}

void Drum(int nod)
{
    if ( t[nod] == 0 )
    {
        printf("%d", a[nod]);
        return;
    }
    Drum(t[nod]);
    printf(" %d", a[nod]);
}