Cod sursa(job #37698)
Utilizator | Data | 25 martie 2007 12:02:19 | |
---|---|---|---|
Problema | Shop | Scor | 20 |
Compilator | fpc | Status | done |
Runda | preONI 2007, Runda 4, Clasa a 9-a si gimnaziu | Marime | 3.23 kb |
type vector=array[0..30,0..2]of longint;
type vector2=array[0..30]of longint;
procedure sort(var d:vector;lo,hi:integer);
var i,j,x,y:integer;
begin
i:=lo;j:=hi;x:=d[(lo+hi)div 2,1];
repeat
while d[i,1]>x do inc(i);
while x>d[j,1] do dec(j);
if i<=j then
begin
y:=d[i,1];d[i,1]:=d[j,1];d[j,1]:=y;
y:=d[i,2];d[i,2]:=d[j,2];d[j,2]:=y;
i:=i+1;
j:=j-1;
end;
until i>j;
if lo<j then sort(d,lo,j);
if i<hi then sort(d,i,hi);
end;
function valid(var k:integer;st:vector2;d:vector;p:longint;b:vector2):integer;
var ok,i,s:integer;
begin
ok:=1;
if d[st[k],2]=0 then
ok:=0;
s:=0;
for i:=1 to k do
s:=b[d[st[i],1]]+s;
if s>p then
ok:=0;
valid:=ok;
end;
procedure back(var d:vector;n,c,p:longint;b:vector2;var w:longint);
var k:integer;
st:vector2;
max,i,s,suma:longint;
begin
k:=1;
st[1]:=0;
while k>0 do
begin
if st[k]<n then
begin
st[k]:=st[k]+1;
if valid(k,st,d,p,b)=1 then
begin
d[st[k],2]:=d[st[k],2]-1;
suma:=0;
for i:=1 to k do
suma:=suma+b[d[st[i],1]];
if suma=p then
begin
for i:=1 to k do
max:=max+d[st[k],1];
w:=k;
k:=-200;
end
else
begin
inc(k);
st[k]:=0;
end;
end;
end
else
begin
dec(k);
d[st[k],2]:=d[st[k],2]+1;
end;
end;
end;
var f:text;
n,c,p,i,s,w,j:longint;
d,a:vector;
b:vector2;
begin
assign(f,'shop.in');
reset(f);
read(f,n,c,p);
for i:=1 to n do
begin
read(f,a[i,1],a[i,2]);
d[i,1]:=a[i,1];d[i,2]:=a[i,2];
end;
close(f);
b[0]:=1;
for i:=1 to 10000 do
begin
b[i]:=b[i-1]*c;
if b[i]>1000000 then
break;
end;
sort(d,1,n);
s:=0;
i:=1;
back(d,n,c,p,b,w);
assign(f,'shop.out');
rewrite(f);
writeln(f,w);
for i:=1 to n do
begin
for j:=1 to n do
if a[i,1]=d[j,1] then
begin
if i=n then
write(f,a[i,2]-d[j,2])
else
write(f,a[i,2]-d[j,2],' ');
break;
end;
end;
close(f);
end.