1033 to fill or not to fill

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1033 to fill or not to fill相关的知识,希望对你有一定的参考价值。

贪心算法

注意两点:

1、sum用double类型

2、第二个测试点是起点没有加油站的情况

AC代码

  1 #include <vector>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <map>
  5 using namespace std;
  6 class gas{
  7 public:
  8     double p;
  9     double d;
 10 };
 11 bool operator<(const gas& a,const gas& b){
 12     return a.d < b.d;
 13 }
 14 int main(){
 15     int c,d,avg,n;
 16     scanf("%d %d %d %d",&c,&d,&avg,&n);
 17     double D(c * avg);
 18     vector<gas> g;
 19     map<double,gas> minG; 
 20     for(int i = 0;i < n;i++){
 21         gas tmp;
 22         scanf("%lf %lf",&tmp.p,&tmp.d);
 23         if(minG.find(tmp.d) == minG.end() || minG[tmp.d].p > tmp.p)
 24             minG[tmp.d] = tmp;
 25     }
 26     for(map<double,gas>::iterator ite = minG.begin();ite != minG.end();ite++){
 27         g.push_back(ite->second);
 28     }
 29     //sort(g.begin(),g.end());
 30     int currentId(0);
 31     float rem(0);
 32     double sum(0);
 33     if(g[0].d > 0){
 34         printf("The maximum travel distance = 0.00\n");
 35         return 0;
 36     }
 37     for(int i = 0;i < g.size();i++){
 38         if(i != g.size() - 1){
 39             if(g[i + 1].d < d && g[i + 1].d - g[i].d > D || g[i + 1].d > d && d - g[i].d > D){
 40                 printf("The maximum travel distance = %.2lf\n",g[i].d + D);
 41                 return 0;
 42             }
 43             if(g[i].d + D >= d){
 44                 int j;
 45                 for(j = i + 1;j < g.size() && g[j].d < d;j++){
 46                     if(g[j].p < g[i].p)
 47                         break;
 48                 }
 49                 if(j == g.size() || g[j].p >= g[i].p){
 50                     if(rem >= d - g[i].d)
 51                         printf("%.2lf\n",sum);
 52                     else
 53                         printf("%.2lf\n",sum + (d - g[i].d - rem) / avg * g[i].p);
 54                     return 0;
 55                 }
 56             }
 57             int j;
 58             bool s1(false);
 59             double minP;
 60             int minId;
 61             for(j = i + 1;j < g.size() && g[j].d <= g[i].d + D;j++){
 62                 if(j == i + 1 || g[j].p < minP){
 63                     minP = g[j].p;
 64                     minId = j;
 65                 }
 66                 if(g[j].p < g[i].p){
 67                     if(rem < g[j].d - g[i].d){
 68                         sum += (g[j].d - g[i].d - rem) / avg * g[i].p ;
 69                         rem = 0;
 70                         i = j - 1;
 71                         s1 = true;
 72                         break;
 73                     }
 74                 }
 75             }
 76             if(s1) continue;
 77             if(j != g.size()){
 78                 sum += (D - rem) / avg * g[i].p;
 79                 rem = D - (g[minId].d - g[i].d);
 80                 i = minId - 1;
 81             }
 82             else{
 83                 if(g[j - 1].p < g[i].p){
 84                     if(g[j - 1].d - g[i].d > rem){
 85                         sum += (g[j - 1].d - g[i].d - rem) / avg * g[i].p;
 86                         rem = 0;
 87                         i = j - 2;
 88                     }
 89                     else{
 90                         rem -= g[j - 1].d - g[i].d;
 91                         i = j - 2;
 92                     }
 93                 }
 94                 else{
 95                     sum += (D - rem) / avg * g[i].p;
 96                     rem = D - (g[j - 1].d - g[i].d);
 97                     i = j - 2;
 98                 }
 99             }
100             continue;
101         }
102         else{
103             if(g[i].d < d - D){
104                 printf("The maximum travel distance = %.2lf\n",g[i].d + D);
105                 return 0;
106             }
107             else{
108                 if(rem >= d - g[i].d)
109                     printf("%.2lf\n",sum);
110                 else
111                     printf("%.2lf\n",sum + (d - g[i].d - rem) / avg * g[i].p );
112                 return 0;
113             }
114         }
115     }
116 }

 

以上是关于1033 to fill or not to fill的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1033. To Fill or Not to Fill

1033 To Fill or Not to Fill (25 分)

1033. To Fill or Not to Fill (25)

1033 to fill or not to fill

PAT 1033 To Fill or Not to Fill[dp]

1033. To Fill or Not to Fill (25)