PAT 1033. To Fill or Not to Fill
来源:互联网 发布:iis php mysql 编辑:程序博客网 时间:2024/05/16 09:19
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: 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 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 27.10 07.00 600Sample 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;}
- 1033. To Fill or Not to Fill (25)-PAT
- 【PAT】1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill
- PAT 1033. To Fill or Not to Fill
- PAT A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (贪心)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- PAT (Advanced) 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- pat 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill
- 【PAT甲级】1033. To Fill or Not to Fill (25)
- 拦截导弹问题
- react生命周期函数
- 对数据库进行CRUD
- 加载图片错乱
- centoos安装rabbitmq
- PAT 1033. To Fill or Not to Fill
- 跑马灯来回跑
- 【转】C++顺序性容器、关联性容器与容器适配器
- 集合最终讲解
- 数据挖掘
- oracle中常用到的权限
- Java小白起步_S3(CSS)
- 消息总线(MQ)知多少
- Codeforces 851B. Arpa and an exam about geometry 计算几何,结论题