比赛 NOIP2008集训模拟1 评测结果 WWWWAAWWWW
题目名称 地精贸易 最终得分 20
用户昵称 zhai 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 11:04:58
显示代码纯文本
program goblin;
  const
    max=100;
  type
    sz=array[0..max,1..2]of integer;
    sx=array[0..max,1..2]of real;
  var
    f1,f2:text;
    n,ans:longint;
    m:integer;
    a,c,e:sz;
    b:sx;
    procedure ini;
      var
        i,j:integer;
      begin
        assign(f1,'goblin.in');reset(f1);
        assign(f2,'goblin.out');rewrite(f2);
        readln(f1,n,m);
        ans:=0;
        fillchar(c,sizeof(c),0);
        fillchar(e,sizeof(e),0);
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        for i:=1 to m do
          for j:=1 to 2 do read(f1,a[i,j]);
        close(f1);
        for i:=1 to m do
          for j:=1 to 2 do e[i,j]:=i;
        for i:=1 to m do
          if a[i,1]>=a[i,2] then b[i,2]:=(a[i,1]-a[i,2])/a[i,2]
          else b[i,1]:=(a[i,2]-a[i,1])/a[i,1];
      end;
    procedure pai(b:sx);
      var
        i,j,p,q:integer;
        x,t:real;
      begin
        for i:=1 to m-1 do begin
          x:=b[i,1];p:=i;
          for j:=i+1 to m do
            if x<b[j,1] then begin
              x:=b[j,1];
              p:=j;
            end;
          t:=b[i,1];b[i,1]:=b[p,1];b[p,1]:=t;
          q:=e[i,1];e[i,1]:=p;e[p,1]:=q;
        end;
        for i:=1 to m do
          if b[e[i,1],1]=b[e[i+1,1],1] then begin
            if a[e[i,1],1]>a[e[i+1,1],1] then begin
            p:=e[i,1];e[i,1]:=e[i+1,1];e[i+1,1]:=p;end;
          end;
        for i:=1 to m-1 do begin
          x:=b[i,2];p:=i;
          for j:=i+1 to m do
            if x<b[j,2] then begin
              x:=b[j,2];
              p:=j;
            end;
          t:=b[i,2];b[i,2]:=b[p,2];b[p,2]:=t;
          q:=e[i,2];e[i,2]:=p;e[p,2]:=q;
        end;
        for i:=1 to m do
          if b[e[i,2],2]=b[e[i+1,2],2] then begin
            if a[e[i,2],2]>a[e[i+1,2],2] then begin p:=e[i,2];e[i,2]:=e[i+1,2];e[i+1,2]:=p;end;
          end;
      end;
    procedure main;
      var
        i,j:integer;
        x:real;
        p,q:longint;
      begin
        p:=n;
        q:=0;
        for i:=1 to m do
          if (b[e[i,1],1]>0)and(p>a[e[i,1],1])then begin
            c[e[i,1],1]:=p div a[e[i,1],1];
            p:=p mod a[e[i,1],1];
          end;
        for i:=1 to m do inc(q,c[i,1]*a[i,2]);
        q:=q+p;
        for i:=1 to m do
          if (b[e[i,2],2]>0)and(q>a[e[i,2],2])then begin
            c[e[i,2],2]:=q div a[e[i,2],2];
            q:=q mod a[e[i,2],2];
          end;
        for i:=1 to m do inc(ans,c[i,2]*a[i,1]);
        ans:=ans+q;
      end;
    procedure pr;
      var
        i:integer;
      begin
        writeln(f2,ans-n);
        for i:=1 to m do
          if c[i,1]<>0 then writeln(f2,'Buy ',c[i,1],' from Alliance')
          else if c[i,2]<>0 then writeln(f2,'Buy ',c[i,2],' from Horde')
          else writeln(f2,'Buy 0');
      end;
  begin
    ini;
    pai(b);
    main;
    pr;
    close(f2);
  end.