比赛 07级noip练习1 评测结果 AAAAAAAAAA
题目名称 纪念品分组 最终得分 100
用户昵称 zhai 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-22 21:45:34
显示代码纯文本
program group;
  type
    sz=array[1..30000]of integer;
  var
    f1,f2:text;
    n,ans:longint;
    w:integer;
    a:sz;
    procedure ini;
      var
        i:longint;
      begin
        assign(f1,'group.in');reset(f1);
        assign(f2,'group.out');rewrite(f2);
        readln(f1,w);
        readln(f1,n);
        for i:=1 to n do read(f1,a[i]);
        close(f1);
      end;
    procedure qu(left,right:longint);
      var
        l,r,x,y,d:longint;
      begin
        l:=left;r:=right;
        x:=a[(l+r)div 2];
        repeat
          while a[l]<x do inc(l);
          while a[r]>x do dec(r);
          if l<=r then
            begin
              y:=a[l];
              a[l]:=a[r];
              a[r]:=y;
              inc(l);
              dec(r);
            end;
        until l>r;
        if (l<right)then qu(l,right);
        if (r>left)then qu(left,r);
      end;
    procedure main;
      var
        h,t:longint;
      begin
        h:=1;t:=n;
        ans:=0;
        repeat
          if a[h]+a[t]<=w then inc(h);
            inc(ans);
            dec(t);
        until abs(t-h)<=1;
        if t-h=0 then inc(ans)
        else begin
          if a[t]+a[h]<=w then inc(ans)
          else inc(ans,2);
        end;
        write(f2,ans);
        close(f2);
      end;
  begin
    ini;
    qu(1,n);
    main;
  end.