记录编号 2602 评测结果 AAAAAAAAAA
题目名称 [NOIP 2007]纪念品分组 最终得分 100
用户昵称 Gravatarbly1991 是否通过 通过
代码语言 Pascal 运行时间 0.053 s
提交时间 2008-09-23 13:37:02 内存使用 0.23 MiB
显示代码纯文本
program bly(input,ouput);
type
  t1=array[1..30000] of longint;
var
  f1,f2:text;
  g:t1;
  i,j,left,right:longint;
  n,s,ans,m:longint;

  procedure Sort(l, r: longint);
  var
    i, j, x, y: integer;
  begin
    i := l; j := r; x := g[(l+r) DIV 2];
    repeat
      while g[i] < x do i := i + 1;
      while x < g[j] do j := j - 1;
      if i <= j then
      begin
        y := g[i]; g[i] := g[j]; g[j] := y;
        i := i + 1; j := j - 1;
      end;
    until i > j;
    if l < j then Sort(l, j);
    if i < r then Sort(i, r);
  end;

begin
  assign(f1,'group.in');
  reset(f1);
  readln(f1,s);
  readln(f1,n);
  for i:=1 to n do readln(f1,g[i]);
  close(f1);

  sort(1,n);
  left:=1; right:=n; m:=0; ans:=0;
  repeat
    if g[left]+g[right]>s then begin
      dec(right); inc(ans); m:=m+1;
    end;
    if g[left]+g[right]<=s then begin
      inc(left); dec(right); inc(ans); m:=m+2;
    end;
  until (left=right)or(left=right+1);
  assign(f2,'group.out');
  rewrite(f2);
  if m+1=n then writeln(f2,ans+1);
  if m=n then writeln(f2,ans);
  close(f2);
end.