P1094 纪念品分组

来源:互联网 发布:php判断是否点击submit 编辑:程序博客网 时间:2024/05/22 15:54

题目描述

有n个礼物,给出每个礼物的价格,按价格分组,每组最多两个礼物,每组礼物价格不能超过M,求最小分多少组。

样例输入

100 9 90 20 20 30 50 60 70 80 90

样例输出

6

思路

排序,从小到大,看看小的和大的最多组成的个数。
var  n:longint;  a:array[1..30000] of longint;procedure qsort(l,r:longint);var  i,j,key,temp:longint;begin  if l>=r then exit;  i:=l;j:=r;  key:=a[l+random(r-l+1)];  repeat    while  (a[i]<key) do inc(i);    while  (a[j]>key) do dec(j);    if i<=j then      begin        temp:=a[i];a[i]:=a[j];a[j]:=temp;        inc(i);dec(j);      end;  until i>j;  qsort(l,j);  qsort(i,r);end;var  i,j,m,p:longint;begin  readln(m);  readln(n);  for i:=1 to n do readln(a[i]);  randomize;  qsort(1,n);  j:=n;i:=1;  while i<=j do    if a[i]+a[j]<=m then      begin inc(i);inc(p);dec(j);end    else      if a[j]<=m then        begin dec(j);inc(p);end      else dec(j);  writeln(p);end.
0 0
原创粉丝点击