PAT 1033. To Fill or Not to Fill

来源:互联网 发布:iis php mysql 编辑:程序博客网 时间:2024/05/16 09:19

1033. To Fill or Not to Fill (25)

时间限制
100 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 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 (<=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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00

除了正着从起点开始一步一步贪心到重点

还可以将加油站按价格排序,从最低的开始买,买完油即可覆盖一片范围。然后从第二便宜的买,再次覆盖,直到把全程覆盖完。


注意不要重复覆盖,也不要超过终点即可。

我懒得检查是否全程都覆盖了,选择了遍历完所有加油站。


终点处做了哨兵,减少一点逻辑复杂度

#include <cstdio>#include <algorithm>#include <map>struct GasStation{double pos;double price;};bool PredPrice(const GasStation& left, const GasStation& right){return left.price < right.price;}GasStation stations[500];int main(){double tank, distance, distPerGas;int numGasStation;scanf("%lf %lf %lf %d", &tank, &distance, &distPerGas, &numGasStation);for (int i = 0; i < numGasStation; ++i){scanf("%lf %lf", &stations[i].price, &stations[i].pos);}// 先从价格最低的开始买,买最多,覆盖范围// 然后再从第二低买,不要重复std::sort(stations, stations + numGasStation, PredPrice);// 在pos,买完后油箱有多少油std::map<double, double> record;// 哨兵,终点处直接覆盖record[distance] = 0.0;// 哨兵,起点处没有油record[0.0] = 0.0;double cost = 0.0;for (int i = 0; i < numGasStation; ++i){auto& station = stations[i];double nowPos = station.pos;// 买足够跑到下一波覆盖区域的油// 上一个覆盖点double lastPos = 0.0;double lastGas = record[0.0];// 下一个覆盖点double nextPos = distance;for (const auto& pair : record){if (pair.first > nowPos){nextPos = pair.first;break;}lastPos = pair.first;lastGas = pair.second;}// 是否已经覆盖了上一个点到下一个点的路程?if (lastPos + lastGas*distPerGas >= nextPos)continue;// 没有全覆盖,还需要填点油// 计算到这里油箱有多少    double nowGas = lastGas - (nowPos - lastPos) / distPerGas;nowGas = std::max(nowGas, 0.0);// 买到跑到下一个覆盖点的油 double  buyGas = (nextPos - nowPos - nowGas*distPerGas) / distPerGas;// 油箱可能不够大。。。buyGas = std::min(buyGas, tank - nowGas);// 计钱cost += buyGas*station.price;// 添加覆盖点record[nowPos] = nowGas + buyGas;}// 看看是否有断点,有断点说明跑不到,没断点就是最小价格路线double lastRange = 0.0;for (const auto& pair : record){if (lastRange < pair.first){printf("The maximum travel distance = %0.2f", lastRange);return 0;}lastRange = pair.first + pair.second*distPerGas;}printf("%0.2lf", cost);system("pause");return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 想从事人事方面的工作没经验怎么办 教师资格考试后户籍转走认定怎么办 总经理离职了作为高管的我怎么办 企业换总经理想换供应商应该怎么办 换了晶振时间还快怎么办 职场两个人都想要你的情况下怎么办 造价起步工资太低又结婚了怎么办 3d保存时写入文件出错怎么办 无经验想在工地承包点小活怎么办 一级建造师挂靠后中标后怎么办 用360对系统修补漏洞很慢怎么办 如果美国和俄罗斯开战中国会怎么办 戴牙冠前临时补牙材料掉了怎么办 设备间在业主家里每次上锁怎么办 成都安全员证原件丢了并过期怎么办 记不施工员证书号了怎么办 优易学车学员版登录不了怎么办 先科移动dvd主板坏了怎么办 离职单位不出劳动解除书怎么办 离职后一级建造师注册证怎么办 京牌货车报废挂靠公司不给办怎么办 二建挂靠注册证书到期了怎么办 二建证书挂靠公司不给钱怎么办 二建拿到证书原单位不解锁怎么办 凯云软件清单锁定只读了怎么办 苹果笔记本鼠标触摸板没反应怎么办 苹果笔记本键盘和触摸板失灵怎么办 苹果手机输入密码显示已停用怎么办 苹果7p手机刷机黑屏了怎么办 苹果5s来电接听屏幕卡顿怎么办? 手机摔了一下触屏失灵怎么办 小米手机摔了一下触屏失灵怎么办 苹果6sp触摸ic坏了怎么办 苹果4s屏幕摔裂了怎么办 新换的手机内屏颜色太亮怎么办 苹果手机摔了一下屏幕失灵怎么办 苹果手机6s屏幕坏了怎么办 苹果6s屏幕摔坏了怎么办 苹果手机屏幕进油了屏幕变暗怎么办 苹果手机不小心屏幕进油了怎么办 苹果6充电插口螺丝口坏了怎么办