Cod sursa(job #202276)

Utilizator cypherMircea Grecu cypher Data 7 august 2008 10:02:39
Problema Subsir crescator maximal Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.96 kb
program scmax;
var a,p,b:array[0..100010] of longint;
	n,l:longint;
	
	procedure citire;
	var f:text; i:longint;
	begin
		assign(f,'scmax.in'); reset(f);
		readln(f,n);
		for i:=1 to n do read(f,a[i]);
		close(f);
	end;
		
	procedure main;
	var i,j,st,fin,mid:longint;
	begin
		l:=0; b[0]:=0;
		for i:=1 to n do begin
			j:=0; st:=0; fin:=l;
			while st<=fin do begin
				mid:=(st+fin) shr 1;
				if a[i]>a[b[mid]] then begin
					j:=mid; st:=mid+1;
				end else fin:=mid-1;
			end;
			p[i]:=b[j];
			if (j=l) or (a[i]<a[b[j+1]]) then b[j+1]:=i;
			if (j=l) then inc(l);
		end;
	end;

	procedure scriere;
	var f:text; i:longint;

		procedure scrie(i:longint);
		begin
			if p[i]=0 then write(f,a[i],' ')
			else begin
				scrie(p[i]);
				write(f,a[i],' ');
			end;
		end;

	begin
		assign(f,'scmax.out'); rewrite(f);
		writeln(f,l);
		i:=b[l];
		scrie(i);
		writeln(f);
		close(f);
	end;

BEGIN
	citire;
	main;
	scriere;
END.