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\n");
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\n",totalPrice);
80     }
81     else{
82         printf("The maximum travel distance = %.2f\n",node[now].d+MAX);
83     }
84     return 0;
85 }


原文地址:https://www.cnblogs.com/Mered1th/p/10434674.html

时间: 2024-11-05 21:24:56

1033 To Fill or Not to Fill (25 分)的相关文章

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

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

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

九度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

1142 Maximal Clique (25 分)图

1142 Maximal Clique (25 分) A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted f

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The

【PAT甲级】1003 Emergency (25分)

1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between an

1055 The World&#39;s Richest (25分)

1. 题目 2. 思路 常规题 3. 注意点 注意超时 4. tip 善于使用题目的条件来减少数据量 fill(begin, end, value)注意[begin, end) 2. 代码 #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iostream> // 14:57 - using namespace std; struct p