i,j,w,n,x:longint //w为每组价格上限,x为分组数,n为总件数procedure qsort(l,r:longint)
var i,j,m,p:longint
begin
i:=lj:=rm:=a[(l+r) div 2]
repeat
while a[i]<m do inc(i)
while a[j]>m do dec(j)
if i<=j then
begin p:=a[i]a[i]:=a[j]a[j]:=pinc(i)dec(j)end
until i>j
if i<r then qsort(i,r)
if l<j then qsort(l,j)
endbegin
assign(input,'group.in') rewrite(input)
assign(output,'group.out') rewrite(output)
readln(w)readln(n)
for i:=1 to n do readln(a[i])
qsort(1,n) //用一次快排把纪念品按价格排列,从低到高
i:=1j:=nx:=0 //i指向价格最低的纪念品,j指向最高的
repeat
if i=j then begin inc(x)breakend//如果只剩下一件纪念品,单独为一组
if a[j]+a[i]<=w then begin inc(i)dec(j)inc(x)end //如果两件纪念品之和不超过w就为一组
else begin dec(j)inc(x)end //如果超过w就让j指向的纪念品单独为一组,i不改变
until i>j //直至分完
writeln(x)
close(input) close(output)
end.
varw,n,i,j,ans:longint
a:array[0..30001] of longint
procedure qsort(r,l:longint)
var
i,j,k:longint
begin
i:=r
j:=l
k:=a[(i+j) shr 1]
repeat
while a[i]<k do inc(i)
while a[j]>k do dec(j)
if i<=j then begin
a[0]:=a[i]
a[i]:=a[j]
a[j]:=a[0]
inc(i)
dec(j)
end
until i>j
if i<l then qsort(i,l)
if r<j then qsort(r,j)
end
begin
readln(w)
readln(n)
for i:=1 to n do readln(a[i])
qsort(1,n)
i:=1j:=n
while i<=j do begin
if i=j then begin
if a[i]<=w then inc(ans)
break
end
if a[i]+a[j]<=w then begin
inc(ans)
inc(i)
dec(j)
end else if a[j]<=w then begin
inc(ans)
dec(j)
end else dec(j)
end
writeln(ans)
end.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)