1033 To Fill or Not to Fill (25 分)

Posted mered1th

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C?max?? (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D?avg?? (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P?i??, the unit gas price, and D?i?? (D), the distance between this station and Hangzhou, for i=1,?,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00


分析:贪心,开始没做出来,后面参考晴神宝典。

假设当前加油站编号为now,每次从当前点开始搜索最大距离范围内的下一个要前往的加油站。

分3种情况:
1、找到范围内第一个比当前加油站价格低的加油站k,加恰好能到达k的油,前往k。
2、如果找不到比当前加油站价格低的加油站,则
找范围内最小价格的加油站k,在当前加油站加满油,前往k。
3、如果在满油状态下都找不到能到达的加油站,则最远能到达的距离为当前加油站的距离加上满油状态下能前进的距离,结束算法

 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-25-22.01.39
 6 * Description : A1033
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=510;
20 const int INF=1000000000;
21 struct Node{
22     double price;
23     double d;
24 }node[maxn];
25 bool cmp(Node a,Node b){
26     return a.d<b.d;
27 }
28 int main(){
29 #ifdef ONLINE_JUDGE
30 #else
31     freopen("1.txt", "r", stdin);
32 #endif
33     int n;
34     double cmax,d,avg;
35     cin>>cmax>>d>>avg>>n;
36     for(int i=0;i<n;i++){
37         scanf("%lf %lf",&node[i].price,&node[i].d);
38     }
39     node[n].price=0;
40     node[n].d=d;  //终点
41     sort(node,node+n,cmp);
42     if(node[0].d!=0){
43         printf("The maximum travel distance = 0.00
");
44         return 0;
45     }
46     int now=0; //当前加油站编号
47     double totalPrice=0,nowTank=0,MAX=cmax*avg;//总花费、当前油量、最大行驶距离
48     while(now < n){
49         int k=-1; //最低油价的加油站编号
50         double priceMin=INF;
51         for(int i=now+1;i<=n&&node[i].d-node[now].d<=MAX;i++){ //在最大行驶距离之内找到油价最低的站
52             if(node[i].price<priceMin){
53                 priceMin=node[i].price;
54                 k=i;
55                 //如果找到第一个油价低于当前油价的加油站,直接去那个加油站加油(跳出循环)
56                 if(priceMin<node[now].price){
57                     break;
58                 }
59             }
60         }
61         if(k==-1) break; //没找到,跳出循环输出最大行驶距离
62         double need=(node[k].d-node[now].d)/avg;
63         if(priceMin<node[now].price){
64             if(nowTank<need){
65                 totalPrice+=(need-nowTank)*node[now].price;
66                 nowTank=0;
67             }
68             else{
69                 nowTank-=need;
70             }
71         }
72         else{
73             totalPrice+=(cmax-nowTank)*node[now].price;
74             nowTank=cmax-need;
75         }
76         now=k;
77     }
78     if(now==n){
79         printf("%.2f
",totalPrice);
80     }
81     else{
82         printf("The maximum travel distance = %.2f
",node[now].d+MAX);
83     }
84     return 0;
85 }

 



以上是关于1033 To Fill or Not to Fill (25 分)的主要内容,如果未能解决你的问题,请参考以下文章

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)