Cod sursa(job #590128)

Utilizator tibi9876Marin Tiberiu tibi9876 Data 15 mai 2011 17:04:25
Problema Zone Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.46 kb
var a:array[1..512,1..512] of int64;
    b,d:array[1..9] of int64;
    c:array[1..512,0..512] of int64;
    v,s:array[0..512] of int64;
    p,l,q,t,i,j,n,r:longint;
    x:int64;

function cautare(k:int64):longint;
var p,u,m:integer;
begin
p:=1;
u:=n;
while p<=u do
begin
m:=(p+u) div 2;
if s[m]=k then break
else if k<s[m] then u:=m-1
else p:=m+1;
end;
if p<=u then cautare:=m else cautare:=0;
end;

function cautare2(k:int64):longint;
var p,u,m:integer;
begin
p:=1;
u:=n;
while p<=u do
begin
m:=(p+u) div 2;
if c[r,m]=k then break
else if k<c[r,m] then u:=m-1
else p:=m+1;
end;
if p<=u then cautare2:=m else cautare2:=0;
end;

procedure solve;
begin
x:=b[1]+b[2]+b[3];
p:=cautare(x);r:=p;
if p=0 then exit;
x:=x+b[4]+b[5]+b[6];
q:=cautare(x);
if q=0 then exit;
l:=cautare2(b[1]);
if l=0 then exit;
t:=cautare2(b[1]+b[2]);
if t=0 then exit;
write(p,' ',q,' ');
writeln(l,' ',t);
halt;
end;

procedure back(k:integer);
var i:integer;
begin
for i:=1 to 9 do
if v[i]=0 then
begin
b[k]:=d[i];
v[i]:=1;
if k=9 then solve
else back(k+1);
v[i]:=0;
end;
end;


begin
assign(input,'zone.in');reset(input);
assign(output,'zone.out');rewrite(output);
readln(n);
for i:=1 to 9 do read(d[i]);
for i:=1 to n do
begin
s[i]:=s[i-1];
for j:=1 to n do
begin
read(a[i,j]);
inc(s[i],a[i,j]);
end;
end;
for p:=1 to n do
for j:=1 to n do
begin
c[p,j]:=c[p,j-1];
for i:=1 to p do
inc(c[p,j],a[i,j]);
end;
back(1);
end.