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 }
时间: 2024-12-20 19:04:51

1033 to fill or not to fill的相关文章

PAT 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1.首先计算从本加油点做多可能达到的加油点 2.如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜的加油点,如果有的话就跑到该便宜的加油点,油量加到支持到该加油点即可 2.2 如果没有比当前加油点更便宜的加油点的话,又分两种情况: 2.2.1.如果本次加油可能直接到终点,就加可以到达终点的油量 2.2.2.否则的话,加满 3.如果没有可达加油点的话: 3.1 看是否可以直接达到终点 3.2 不能达

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to fin

PAT 1033 To Fill or Not to Fill[dp]

1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may gi

1033 To Fill or Not to Fill (25 分)

1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may g

pat1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to fin

九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way fro

PAT 1033. To Fill or Not to Fill

#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int main() { int N; double mcap, dist, davg; scanf("%lf%lf%lf%d", &mcap, &dist, &davg, &N); double price, id

PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { double cost, x, v; int

1033. To Fill or Not to Fill (25) -贪心算法

题目如下: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are