NOJ[1272] Smart Cat

  • 问题描述
  • Long long ago,there is a smart fat cat, she likes eating fish very much. One day the cat decided to go to fish, she brings a bucket, it can load at most N kg things.After a day's work, the cat caught T fishes, each fish has its own weight and delicious degree.Obviously the cat can not take all of them away.The cat is smart but greedy, so she always picks up the most valuable fish(if she can).

    • 输入
    • Each test case will begin with a integer N(1 <= N <= 125000)and a integer T(1 <= T <= 50). then three lines follow, means weight、value and number, each number ranges from 1 to 100.
    • 输出
    • For each test case,output the sum of delicious degree in the bucket.
    • 样例输入
    • 7 4
      2 4 3 1
      3 2 4 1
      1 1 1 1
      9 4
      1 2 2 1
      1 4 6 4
      10 4 1 3
    • 样例输出
    • 8
      26
    • 提示
    • More ddetails: if two fish have the same "value", take the heavier one.
    • 来源
    • Mr.Cai   


  • 输入
  • Each test case will begin with a integer N(1 <= N <= 125000)and a integer T(1 <= T <= 50). then three lines follow, means weight、value and number, each number ranges from 1 to 100.
  • 输出
  • For each test case,output the sum of delicious degree in the bucket.
    More ddetails: if two fish have the same "value", take the heavier one.

  • 样例输入
  • 7 4
    2 4 3 1
    3 2 4 1
    1 1 1 1
    9 4
    1 2 2 1
    1 4 6 4
    10 4 1 3
  • 样例输出
  • 8
    26
  • 是不是有多重背包的错觉,可惜那只是错觉
  • 看那红字,发现什么了,对,贪心!
  • 然后,排序
  • 显然是按价值从大到小排的,然而,此价值非彼价值,是按价值-重量比来排的
  • 然后,当性价比一样时,再按重量排序
  • 代码:
  • #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    
    using namespace std;
    
    struct FISH
    {
      int weight,value,num;
    }fish[60];
    
    int cmp(FISH a,FISH b)
    {
       double x=a.value*1.0 / a.weight;
       double y=b.value*1.0 / b.weight;
       if(fabs(x-y)<1e-5) 
         return a.weight > b.weight;
       return x>y;
    }
    
    int main()
    {
      int t;
      long long n;
      while(~scanf("%lld%d",&n,&t))
      {
        int i;
        int cnt=0;
        for(i=0;i<t;i++)
          scanf("%d",&fish[i].weight);
        for(i=0;i<t;i++)
          scanf("%d",&fish[i].value);
        for(i=0;i<t;i++)
          scanf("%d",&fish[i].num);
        sort(fish,fish+t,cmp);
        for(i=0;i<t;i++)
        {
           if(n>=fish[i].weight)  //可以拿
           {
              for(int j=1;j<=fish[i].num;j++)
              {
                if(n>=fish[i].weight)
                {
                  cnt+=fish[i].value;
                  n-=fish[i].weight;
                }
                else
                  break;
              }
           }
        }
        printf("%d\n", cnt);
      }
      return 0;
    }
    其实本题本身并不难,难就难在sort排序的cmp上,通过本题,相信大家会对cmp的写法有更深入的想法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值