1033 To Fill or Not to Fill

代码如下:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 typedef struct
 7 {
 8     double pos;
 9     double price;
10 }gasstation;
11 gasstation gasst[502];
12 
13 bool cmp(gasstation a,gasstation b)
14 {
15     if(a.pos<b.pos)
16         return true;
17     return false;
18 }
19 
20 int main()
21 {
22     double Cmax,D,Davg;
23     int N;
24     scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
25     int i;
26     for(i=0;i<N;i++)
27         scanf("%lf%lf",&gasst[i].price,&gasst[i].pos);
28     sort(gasst,gasst+N,cmp);
29     if(D==0)
30     {
31         printf("0.00\n");
32         return 0;
33     }
34     if(gasst[0].pos!=0)
35     {
36         printf("The maximum travel distance = 0.00\n");
37         return 0;
38     }
39     int curstnum=0;               //当前所处的油站编号,即当前的位置
40     double curgas=0;              //当前的油量
41     double curcost=0;                //当前的花费
42     bool flag=false;              //是否达到目的
43     double maxrundis=Cmax*Davg;        //邮箱加满最远能行驶的距离
44     while(!flag)
45     {
46         bool tag=false;            //最大距离内是否有加油站
47         bool ifcheaper=false;    //是否有便宜的
48         double cheapestprice=10000;    //找出最便宜的
49         int cheapestnum;        //没有更便宜的情况下,找出最便宜的
50         for(i=curstnum+1;i<N;i++)
51         {
52             if((gasst[i].pos-gasst[curstnum].pos)<=maxrundis)    //范围内
53             {
54                 tag=true;         //有加油站
55                 if(gasst[i].price<gasst[curstnum].price)        //情况3-a
56                 {                                            //且有更便宜的
57                     ifcheaper=true;
58                     double dist=gasst[i].pos-gasst[curstnum].pos;
59                     double needgas=dist/Davg-curgas;
60                     curgas=0;
61                     curcost+=(needgas*gasst[curstnum].price);
62                     curstnum=i;
63                     break;
64                 }
65                 if(gasst[i].price<cheapestprice)
66                 {
67                     cheapestprice=gasst[i].price;
68                     cheapestnum=i;
69                 }
70             }
71             else
72                 break;
73         }
74         if(!ifcheaper&&(maxrundis>=(D-gasst[curstnum].pos)))   //说明已经可以到达目的地了,情况1
75         {
76             double dist=D-gasst[curstnum].pos;
77             double needgas=dist/Davg-curgas;
78             curcost+=needgas*gasst[curstnum].price;
79             printf("%.2lf\n",curcost);
80             return 0;
81         }
82         if(tag&&!ifcheaper)            //情况3-b
83         {
84             double needgas=Cmax-curgas;
85             curcost+=(needgas*gasst[curstnum].price);
86             double dist=gasst[cheapestnum].pos-gasst[curstnum].pos;
87             curgas=Cmax-dist/Davg;
88             curstnum=cheapestnum;
89         }
90         else if(!tag)                        //情况2
91         {
92             printf("The maximum travel distance = %.2lf\n",gasst[curstnum].pos+maxrundis);
93             return 0;
94         }
95     }
96     return 0;
97 }

 

posted @ 2018-10-08 16:40  琥琥笙威  阅读(194)  评论(0编辑  收藏  举报