比赛 NOIP2008集训模拟1 评测结果 ATTTAEEEWW
题目名称 地精贸易 最终得分 20
用户昵称 bly1991 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 11:09:41
显示代码纯文本
program bly(input,output);
type
  t1=array[0..100000] of integer;
var
  a,b:array[0..100000] of integer;
  c:array[0..100000] of integer;
  s,temp1,temp2:t1;
  n,m,max1,max2,ans,z:integer;
  i,j:integer;
  f1,f2:text;

  function pd1(x,y:integer;g:t1):integer;
  var
    i0,money:integer;
  begin
    money:=m;
    g[x]:=y;
    for i0:=1 to n do money:=money-a[i0]*g[i0];
    pd1:=money;
  end;

  procedure dg1(x,money:integer;g:t1);
  var
    i0:integer;
  begin
    if x<=n then begin
      ans:=0;
      for i0:=1 to n do ans:=ans+(b[i0]-a[i0])*g[i0];
      if ans>=max1 then begin max1:=ans; temp1:=g; end;
      for i0:=0 to c[x] do begin
        z:=pd1(x,i0,g);
        if z>=0 then begin
          g[x]:=i0;
          dg1(x+1,z,g);
        end;
      end;
    end;
  end;

  function pd2(x,y:integer;g:t1):integer;
  var
    i0,money:integer;
  begin
    money:=m;
    g[x]:=y;
    for i0:=1 to n do money:=money-b[i0]*g[i0];
    pd2:=money;
  end;

  procedure dg2(x,money:integer;g:t1);
  var
    i0:integer;
  begin
    if x<=n+1 then begin
      ans:=0;
      for i0:=1 to n do ans:=ans+(a[i0]-b[i0])*g[i0];
      if ans>=max2 then begin max2:=ans; temp2:=g; end;
      for i0:=0 to c[x] do begin
        z:=pd2(x,i0,g);
        if z>=0 then begin
          g[x]:=i0;
          dg2(x+1,z,g);
        end;
      end;
    end;
  end;

begin
  assign(f1,'goblin.in');
  reset(f1);
  readln(f1,m,n);
  for i:=1 to n do readln(f1,a[i],b[i]);
  close(f1);

  max1:=0; max2:=0;
  for i:=1 to n do begin if b[i]-a[i]>0 then c[i]:=m div a[i]; end;
  dg1(1,m,s);
  m:=m+max1;
  for i:=1 to n do c[i]:=0;
  for i:=1 to n do begin if a[i]-b[i]>0 then c[i]:=m div b[i]; end;
  dg2(1,m,s);

  assign(f2,'goblin.out');
  rewrite(f2);
  writeln(f2,max2+max1);
  for i:=1 to n do begin
    if (temp2[i]=0)and(temp1[i]<>0) then writeln(f2,'Buy ',temp1[i],' ','from Alliance');
    if (temp1[i]=0)and(temp2[i]<>0) then writeln(f2,'Buy ',temp2[i],' ','from Horde');
    if (temp1[i]=0)and(temp2[i]=0) then writeln(f2,'Buy ','0');
  end;
  close(f2);
end.