PAT甲级——A1033 To Fill or Not to Fill
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: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤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: Pi, the unit gas price, and Di (≤), the distance between this station and Hangzhou, for ,. 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
解题思路:
步骤1:把终点视为单位油价为0、离起点距离为D的加油站,然后将所有加油站按离起点的距离从小到大进行排序。排序完毕后,如果离起点最近的加油站的距离不是0,则表示汽车无法出发(初始时刻油量为0),输出“The maximum travel distance = 0.00”;如果离起点最近的加油站的距离是0(即加油站就在起点),则进入步骤2。
步骤2:假设当前所处的加油站编号为now,接下来将从满油状态下能到达的所有加油站中选出下一个前往的加油站,策略如下:
①寻找距离当前加油站最近的油价低于当前油价的加油站(记为k),加恰好能够到达加油站k的油,然后前往加油站k(即优先前往更低油价的加油站)。
②如果找不到油价低于当前油价的加油站,则寻找油价最低的加油站,在当前加油站加满油,然后前往加油站k(即在没有更低油价的加油站时,前往油价尽可能低的加油站)。
③如果在满油状态下都找不到能到达的加油站,则最远能到达的距离为当前加油站的距离加上满油状态下能前进的距离,结束算法(即没有加油站可以到达时结束算法)。
上面的策略当满足条件③、或者到达加油站n(即终点)时结束。其中①和②的证明如下。策略①的证明:假设三个加油站的顺序为a、b、c(当前在加油站a),且油价大小为a > b(与c的油价大小无关),则先从a加能到达b的油,然后在b加能到达c的油,要比直接从a加能到达c的油要节省(因为a的油价比b高)。因此,在所有能到达的加油站中,总是优先选择最近的油价低于当前油价的加油站。
策略②的证明:假设三个加油站的顺序为a、b、c(当前在加油站a),且油价大小为a <
b < c,显然应该先在a加满油(因为b、c油价高),然后前往b、c中油价较低的加油站b(如果一定要去c,也应该是先到油价相对便宜的b,然后去c才更划算(从c出发买油价格高,还不如在b先买好))。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main()
{ int N;
double C, D, Dav, dis, price;
cin >> C >> D >> Dav >> N;
vector<pair<double, double>>station;
for (int i = ; i < N; ++i)
{
cin >> price >> dis;
station.push_back(make_pair(dis, price));
}
station.push_back(make_pair(D, ));//将终点也视为一个加油目的地
sort(station.begin(), station.end(), [](pair<int, double>a, pair<int, double>b) {return a.first < b.first; });
if (station[].first != )//起始点没有加油站
printf("The maximum travel distance = 0.00\n");
else
{
int start = ;//出发点
double res = 0.0, nowTank = 0.0;
while (start < station.size())
{
int next = -;
double minPrice = ;
for (int i = start + ; i < station.size() && station[i].first - station[start].first <= C * Dav; ++i)//找到下一家最便宜的加油站
{
if (minPrice > station[i].second)//没有比当前更便宜的加油站,那就找中途最便宜的加油站
{
minPrice = station[i].second;
next = i;
if (station[i].second < station[start].second)//找到比当前便宜点的加油站
break;
} }
if (next == -)//加油站太远,到不到
break;
double need = (station[next].first - station[start].first) / Dav;//需要耗油量
if (minPrice < station[start].second)//比当前油价还便宜,那就不用加满油箱
{
if (nowTank < need)//油不够
{
res += station[start].second *(need - nowTank);
nowTank = ;//油刚好到下一站,到达下一站即没有油了
}
else
nowTank -= need;//油还够,那就不用加了
}
else//中间的加油价比现在的要贵,那就在这里加满
{
res += station[start].second * (C - nowTank);
nowTank = C - need;
}
start = next;
}
if (start == N)//到达终点
printf("%.2f\n", res);
else
printf("The maximum travel distance = %.2f\n", station[start].first + C * Dav);
}
return ;
}
PAT甲级——A1033 To Fill or Not to Fill的更多相关文章
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- PAT 甲级 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 oth ...
- PAT甲级——1033 To Fill or Not to Fill
1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
随机推荐
- php数据结构课程---7、队列实战
php数据结构课程---7.队列实战 一.总结 一句话总结: 注意条件:注意循环的条件(比如while循环打印队列元素时),注意if的条件 把问题想清楚:比如链表操作初次插入元素和后面再插,效果是不一 ...
- PAT甲级——A1122 Hamiltonian Cycle【25】
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 微信小程序 主题皮肤切换(switch开关)
示例效果: 功能点分析: 1.点击switch开关,切换主题皮肤(包括标题栏.底部tabBar):2.把皮肤设置保存到全局变量,在访问其它页面时也能有效果3.把设置保存到本地,退出应用再进来时,依然加 ...
- Java学习之程序运行机制
Java的运行机制是:先编译,后解释. 典型的编译型编程语言是:C和C++. eg:用c语言编写的*.c文件编译生成*.h文件,通过链接将*.h文件链接成*.exe文件,然后就可以运行,编译型的高级语 ...
- SQL语句的四种连接
SQL的四种连接查询 内连接 inner join 或者 join 外连接 左连接 left join 或者 left outer join 右连接 right join 或者 right ou ...
- linux常用软连接使用ln -s
[软连接]另外一种连接称之为符号连接(Symbolic Link),也叫软连接.软链接文件有类似于Windows的快捷方式.它实际上是一个特殊的文件.在符号连接中,文件实际上是一个文本文件,其中包含的 ...
- LeetCode 19.删除链表的倒数第N个节点(Python)
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点 ...
- js怎样把URL链接的参数截取出来
有时候,A页面参数需要传递到B页面,则把参数拼接到跳转B页面的url上,这时怎样在另一个页面截取A页面传递的参数呢,主要代码如下 /** * 获取指定的URL参数值 URL:http://www.qu ...
- thinkphp 组合查询
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string).复合查询(_complex).请求字符串查询(_query),混合查询中的特殊查询每次查询只能定 ...
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...