[ 题解 ] [ 贪心 ] C. Rest Stops (待更名)

http://codeforces.com/group/NVaJtLaLjS/contest/238204/problem/C


题意

农夫和牛去爬山。在山上有N个落脚点,每个落脚点的位置x[i],落脚点的草的美味值为c[i]

牛在落脚点停留t秒,就获得t*c[i]美味值。


现在给出爬山路程L,落脚点数量N,农夫的速率rF,牛的速率rB

(!速率按秒每米给出!保证rF > rB

以及N个点的位置和草的美味值。

要求牛不能落后于农夫,问能在路上能得到的最大美味值是多少。


示例:

Input 

10 2 4 3
7 2
8 1

Output

15

 

看示例猜题意完蛋系列:以为每个落脚点都要吃草,而且要等到农夫过来才能走。这样不就是简单累加出一个固定数值吗?GG


这里只要求得到最大的美味值,直接去吃美味值最大的草就可以了。

比如把示例改为

7 1
8 2

跳过78吃草可以得到更多的美味值。


这思路就是贪心。先贪最大值,在剩下的选择中再贪最大值,直到没有选择为止。


题目给出的数据并没有排序。你要根据美味值来给这些落脚点排序,按美味值降序排列。

struct stops
{
    long distance,tastiness;
}stops[100002]={{0,0}};
int compare(const void *n1,const void *n2)
{
    struct stops *p1=n1,*p2=n2;
    return p2->tastiness - p1->tastiness;
}
qsort(&stops[1],N,sizeof(struct stops),compare);

结构体捆绑位置和美味值;compare函数比较两个结构体谁的美味值大;qsort排序。


排序完美味值大的就排在前面了。用一个tmp记下当前位置0,逐个检查:

这个落脚点的位置大于tmp,那么t=(距离)*(农夫和牛的速率差)表示牛比农夫早t秒到达这个落脚点;

累加时间t*美味值;

记下当前的位置。


答案这不就出来了?


注意这道题得用long long来累加答案,用%I64d输出。long不够用。(CFC编译器实现的long4个字节)


写错了贪心导致代码极杂,这里可以看看另一篇贪心题解,比本题容易很多。

https://www.cnblogs.com/Kaidora/p/10514202.html

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 struct grass
  5 {
  6     long distance,tastiness;
  7 }grass[100002]={{0,0}};
  8 long L,N,rateF,rateB;
  9 //    long grass[10002][2]={0};
 10 long timeF=0,timeB=0;
 11 long long answer=0;
 12 long last=0;
 13 
 14 int compare(const void *n1,const void *n2)
 15 {
 16     struct grass *p1=n1,*p2=n2;
 17     return p2->tastiness - p1->tastiness;
 18 }
 19 
 20 int main()
 21 {
 22     scanf("%ld%ld%ld%ld",&L,&N,&rateF,&rateB);
 23     long tmp=0;
 24     for(int n=1,i=1;i<=N;i++,n++)
 25     {
 26         scanf("%ld",&grass[n].distance);
 27         scanf("%ld",&grass[n].tastiness);
 28 /*
 29         for(;n>1;n--)
 30         {
 31             if(grass[n].tastiness>grass[n-1].tastiness)
 32             {
 33                 grass[n-1].distance=grass[n].distance;
 34                 grass[n].distance=0;
 35                 grass[n-1].tastiness=grass[n].tastiness;
 36                 grass[n].tastiness=0;
 37             }
 38             else break;
 39         }
 40 */
 41     }
 42     
 43     qsort(&grass[1],N,sizeof(struct grass),compare);
 44 
 45 //    for(int n=1;grass[n].distance;n++)
 46 //        printf("%ld,%ld\n",grass[n].distance,grass[n].tastiness);
 47 
 48     for(int n=1;n<=N;n++)
 49     {
 50         if(tmp < grass[n].distance )
 51             answer+= (long long)(rateF-rateB)*( grass[n].distance-tmp )*grass[n].tastiness,tmp=grass[n].distance;
 52     }
 53     
 54     
 55     
 56 /*
 57     for(int n=1,i=1;i<=N;i++)
 58     {
 59         if(grass[i].tastiness > grass2[n].tastiness)
 60         {
 61             grass2[n].distance=grass[i].distance;
 62             grass2[n].tastiness=grass[i].tastiness;
 63             for(;n>1;n--)
 64             {
 65                 if(grass2[n].distance>grass2[n-1].distance)
 66                 {
 67                     grass2[n-1].distance=grass2[n].distance;
 68                     grass2[n].distance=0;
 69                     grass2[n-1].tastiness=grass2[n].tastiness;
 70                     grass2[n].tastiness=0;
 71                 }
 72             }
 73         }    
 74         else
 75         {
 76             n++;
 77             grass2[n].distance=grass[i].distance;
 78             grass2[n].tastiness=grass[i].tastiness;
 79         }
 80     }
 81 */
 82 /*
 83     for(int n=1;grass2[n].distance;n++)
 84     {
 85         printf("%ld,%ld\n",grass2[n].distance,grass2[n].tastiness);
 86     }
 87 */
 88 /*
 89     for(int n=1;grass2[n].distance && n<=N;n++)
 90     {
 91         timeF=rateF*(grass2[n].distance-last);
 92         timeB=rateB*(grass2[n].distance-last);
 93         int dtime=timeF-timeB;
 94         answer+=dtime*grass2[n].tastiness;
 95         last=grass2[n].distance;
 96     }
 97 */    
 98     printf("%I64d\n",answer);
 99     return 0;
100 }
View Code

 

posted @ 2019-03-13 05:02  Kaidora  阅读(191)  评论(0编辑  收藏  举报