比赛 NOIP2008集训模拟1 评测结果 AAWWAAWWWA
题目名称 地精贸易 最终得分 50
用户昵称 elysian 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 10:36:27
显示代码纯文本
program elysian;
type
node=record
x,y:longint;
end;
const
fin='goblin.in';fout='goblin.out';
var
n,m,ans1,ans2,last:longint;
w,c,f:array[0..4000000] of longint;
pre:array[0..4000000] of node;
flag,d:array[0..100] of integer;
f1,f2:text;



procedure main1;
var
i,j:longint;
begin
for i:=1 to m do
if flag[i]=1 then
begin
 for j:=1 to n do
  begin
    if j-c[i]>=0 then { f[j]:=max(f[j],f[j-c[i]]+w[i])}
     if f[j-c[i]]+w[i]>f[j] then
      begin
        f[j]:=f[j-c[i]]+w[i];
        pre[j].x:=j-c[i];
        pre[j].y:=i;
      end;
   if f[j]>=ans1 then begin ans1:=f[j];last:=j;end;
  end;

end;

end;

procedure main2;
var
i,j:longint;
begin
for i:=1 to m do
if flag[i]=2 then
begin
 for j:=1 to n do
  begin
   if j-c[i]>=0 then { f[j]:=max(f[j],f[j-c[i]]+w[i])}
     if f[j-c[i]]+w[i]>f[j] then
      begin
        f[j]:=f[j-c[i]]+w[i];
        pre[j].x:=j-c[i];
        pre[j].y:=i;
      end;
  if f[j]>=ans2 then begin ans2:=f[j];last:=j;end;
  end;
end;

end;

procedure search;
var
i,j,t1,t2:longint;
begin
i:=last;
repeat
t1:=pre[i].x;
t2:=pre[i].y;
if (i-t1>=0)and(c[t2]<>0) then d[t2]:=d[t2]+(i-t1) div c[t2];
i:=t1;
until i<=0;

end;

procedure print;
var
i:longint;
begin
assign(f2,fout);rewrite(f2);
writeln(f2,ans1+ans2);
for i:=1 to m do
if (flag[i]=1)and(d[i]>0) then writeln(f2,'Buy ',d[i],' from Alliance')
else if (flag[i]=2)and(d[i]>0) then writeln(f2,'Buy ',d[i],' from Horde')
else if d[i]=0 then writeln(f2,'Buy 0');
close(f2);

end;

procedure init;
var
i,j,t1,t2:longint;
begin
assign(f1,fin);reset(f1);
readln(f1,n,m);
for i:=1 to m do
begin
readln(f1,t1,t2);
if t2>t1 then begin flag[i]:=1;c[i]:=t1;w[i]:=t2-t1;end;
if t1>t2 then begin flag[i]:=2;c[i]:=t2;w[i]:=t1-t2;end;
end;
close(f1);
end;

begin
init;
ans1:=0;
main1;
fillchar(d,sizeof(d),0);
search;
n:=n+ans1;
fillchar(pre,sizeof(pre),0);
ans2:=0;
fillchar(f,sizeof(d),0);
main2;
search;
print;
end.