营养膳食(贪心算法)

来源:互联网 发布:手机图章制作软件 编辑:程序博客网 时间:2024/04/27 19:34

Description

阿月正在女朋友宁宁的监督下完成自己的增肥计划。 
为了增肥,阿月希望吃到更多的脂肪。然而也不能只吃高脂肪食品,那样的话就会导致缺少其他营养。阿月通过研究发现:真正的营养膳食规定某类食品不宜一次性吃超过若干份。比如就一顿饭来说,肉类不宜吃超过1份,鱼类不宜吃超过1份,蛋类不宜吃超过1份,蔬菜类不宜吃超过2份。阿月想要在营养膳食的情况下吃到更多的脂肪,当然阿月的食量也是有限的。 

Input

输入来自文件diet.in,第一行包含三个正整数n(n≤200),m(m≤100)和k(k≤100)。表示阿月每顿饭最多可以吃m份食品,同时有n种食品供阿月选择,而这n种食品分为k类。第二行包含k个不超过10的正整数,表示可以吃1到k类食品的最大份数。接下来n行每行包括2个正整数,分别表示该食品的脂肪指数ai和所属的类别bi,其中ai≤100,bi≤k。

Output

输出到文件diet.out,包括一个数字即阿月可以吃到的最大脂肪指数和。

Sample Input

 

6 6 3 
3 3 2 
15 1 
15 2 
10 2 
15 2 
10 2 
5 3

 

Sample Output

 

60


解题思路:先读入数据,然后对数据进行快排,再用贪心的思想找出符合题意且脂肪最大的方案,再累加输出即可。


程序:
var
  a,b,c:array [1..200] of longint;
  n,m,k,ans,tot,i,j:longint;

procedure qsort(l,r:longint);
  var
    i,j,mid,temp:longint;
  begin
    i:=l; j:=r; mid:=a[(l+r) div 2];
    repeat
    while a[i]
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
        temp:=b[i]; b[i]:=b[j]; b[j]:=temp;
        inc(i); dec(j);
      end;
    until i>j;
    if i
    if j>l then qsort(l,j);
end;

begin
  readln(n,m,k);
  for i:=1 to k-1 do
    read(c[i]);
  readln(c[i+1]);
  for i:=1 to n do
    readln(a[i],b[i]);
  qsort(1,n);
  i:=m;
  j:=n+1;
  while ((j>1) or (c[b[j]]>0)) and (i>0) do
    begin
      dec(j);
      if c[b[j]]>0 then
        begin
          dec(c[b[j]]);
          inc(ans,a[j]);
          dec(i);
        end
    end;
  writeln(ans);
end.


版权属于: Chris
原文地址: http://blog.sina.com.cn/s/blog_83ac6af80102v0zu.html
转载时必须以链接形式注明原始出处及本声明。
0 0
原创粉丝点击