比赛 07级noip练习1 评测结果 AAAAATATTA
题目名称 纪念品分组 最终得分 70
用户昵称 rottenwood 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-22 22:00:35
显示代码纯文本
program group;
type
 shuzu=array[1..30001] of integer;
 biaozhi=array[1..30001] of boolean;
 var
 t,c,max,i,j,m,n,w,a,k:longint;
 f1,f2:text;
 flag:biaozhi;
 s:shuzu;
 flag1:boolean;
procedure Sort(l,r:longint);
var
  i,j,x,y,t1,t2:longint;
begin
  i:=l; j:=r; x:=s[(l+r) DIV 2];
  repeat
    while s[i]<x do i:=i+1;
    while x<s[j] do j:=j-1;
    if i<=j then
    begin
      y:=s[i]; s[i]:=s[j]; s[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);
assign(f2,'group.out');rewrite(f2);
readln(f1,w);
readln(f1,n);
for i:=1 to n do
  readln(f1,s[i]);
 Sort(1,n);
 fillchar(flag,sizeof(flag),0);
 c:=n;    a:=0;
for i:=n downto 1 do
  begin
  if (not flag[i]) then begin
  j:=1; max:=0; flag1:=true;
  k:=1;
 while (flag[k])and(k<=n) do begin inc(k); end;
 if k>n then begin
 writeln(f2,a);
 close(f2);
 exit;
 end;
  while j<i do begin
   if (flag1)and(s[i]+s[j]=w)and(not flag[i])and(not flag[j]) then begin
                       flag[i]:=true;flag[j]:=true;
                       inc(a);
                       break;
                       end
                  else if
     (s[i]+s[j]<w)and(not flag[i])and(not flag[j]) then  begin
          if s[i]+s[j]>max then begin max:=s[i]+s[j];
                                      t:=j; flag1:=false;
                                end;   end;inc(j);   end;
  if (not flag1)then begin flag[i]:=true; flag[t]:=true; inc(a); end;
  if (j>=i)and(max=0)and(flag1) then begin flag[i]:=true; inc(a); end;
  end;
  end;
writeln(f2,a);
close(f2);
end.