NOJ[1272] Smart Cat

来源:互联网 发布:vue组件引用js插件 编辑:程序博客网 时间:2024/06/05 00:32
  • 问题描述
  • 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 42 4 3 13 2 4 11 1 1 19 41 2 2 11 4 6 410 4 1 3
    • 样例输出
    • 826
    • 提示
    • 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 42 4 3 13 2 4 11 1 1 19 41 2 2 11 4 6 410 4 1 3
  • 样例输出
  • 826
  • 是不是有多重背包的错觉,可惜那只是错觉
  • 看那红字,发现什么了,对,贪心!
  • 然后,排序
  • 显然是按价值从大到小排的,然而,此价值非彼价值,是按价值-重量比来排的
  • 然后,当性价比一样时,再按重量排序
  • 代码:
  • #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的写法有更深入的想法

0 0
原创粉丝点击