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: Cma**x (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Dav**g (≤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
思路
  • 贪心策略

    • 优先前往更低油价的加油站(\(K\)):加到刚好能到\(K\)的油量,加更便宜的油
    • 没有更低油价的:在当前油站处加满
    • 没有加油站可以到达的时候:到极限了
  • 每到新的一个加油站都要根据上面的策略做出决策
代码
#include<bits/stdc++.h>
using namespace std;
struct node
{
double pi, di; //价格和到起点的距离
}a[510];
bool cmp(node a, node b)
{
return a.di < b.di;
} int main()
{
double capacity, distance, run_per_gas;
int stations;
cin >> capacity >> distance >> run_per_gas >> stations;
for(int i=0;i<stations;i++)
cin >> a[i].pi >> a[i].di;
a[stations].pi = 0;
a[stations].di = distance; //将重点堪称油价为0,距离起点为D的加油站
sort(a, a+stations, cmp); if(a[0].di != 0) //一开始的油量为0,如果最近的加油站距离不是0那么肯定不行
{
cout << "The maximum travel distance = 0.00\n";
return 0;
}
int pos = 0; //当前所在的加油站
double best_len = capacity * run_per_gas; //满油状态下能走得最远的距离
double gas_owned = 0.0; //当前状态之下有的油量
double cheapest = 0.0; //最后要花费的油钱
while(pos < stations)
{
int next = -1;
double min_price = 2100000000;
for(int i=pos+1;i<=stations && a[i].di - a[pos].di <= best_len;i++) //当前油量能走的最远距离
{
if(a[i].pi < min_price)
{
min_price = a[i].pi;
next = i;
if(min_price < a[pos].pi)
break;
}
}
if(next == -1) break; double need = (a[next].di - a[pos].di) / run_per_gas; //所需油量
if(min_price < a[pos].pi) //加油站k的油价小于当前油价
{
if(gas_owned < need) //油量不足
{
cheapest += (need - gas_owned) * a[pos].pi; //补足然后去下个更便宜的加油站
gas_owned = 0;
}else gas_owned -= need;
}else
{
cheapest += (capacity - gas_owned) * a[pos].pi;
gas_owned = capacity - need;
}
pos = next;
}
if(pos == stations)
printf("%.2f\n", cheapest);
else
printf("The maximum travel distance = %.2f\n", a[pos].di + best_len);
return 0;
}
引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080

PTA(Advanced Level)1033.To Fill or Not to Fill的更多相关文章

  1. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  2. PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

    贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1003 Emergency

    Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...

  5. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  6. PTA (Advanced Level) 1018 Public Bike Management

    Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...

  7. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  8. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  9. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

随机推荐

  1. Bzoj 1086: [SCOI2005]王室联邦(分块)

    1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special Judge Submit: 1557 Solved: 9 ...

  2. Python使用grequests并发发送请求

    目录 前言 grequests简单使用 grequests和requests性能对比 异常处理 前言 requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快.但是 ...

  3. Java三大特征--多态

    1.定义 允许不同类的对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式. 2.存在条件 2.1存在继承关系 2.2子类重写了父类方法 2.3父类类型的变量指向子类对象的 ...

  4. CF1030C

    CF1030C 题意: 给你一个数字,问能否拆分成k段,使得每一段的每一位数字相加结果相等. 解法: 考虑数位DP. 暴力按位考虑每一位是否满足条件 CODE: #include<cstdio& ...

  5. 修复grub rescue问题

    前几天,手欠点了下win10的系统升级,直接从17.09升级到了19.3虽然也有些波折,总体顺利,以为一切都完事大吉之时,重启系统,原来,万恶的win10给我挖好了坑,早等着我呢.我去,千万只cnm脑 ...

  6. 2019PKUWC游记

    有的时候,不是你不会 而是你,认为你不会 ——*Miracle* 本篇游记就简单写了 Day-inf 犹豫许久,还是选择了北大 不是因为喜欢——甚至恰好相反 而是,听说清华高手较多,约型单一, 于是我 ...

  7. IntelliJ跳转到抽象方法的实现

    ctrl + b (等价于ctrl + 鼠标点击方法名)会调到这个类型的抽象方法中: 如果想要跳转到这个方法的具体实现可以使用 ctrl + alt + 鼠标点击方法名. IntelliJ快速查找一个 ...

  8. 蜗牛圈圈-时尚智能的运动计时App

    Duang! 各类运动爱好者的福音来啦! 蜗牛圈圈-最智能的圈速计时助手 扫描二维码下载体验 [产品简介] -蜗牛圈圈是一款专业的圈速计时工具,帮助您获得整个运动过程中的各项数据,保存记录,分享激情. ...

  9. PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句

    SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...

  10. js闭包小实验

    js闭包小实验 一.总结 一句话总结: 闭包中引用闭包外的变量会使他们常驻内存 function foo() { var i=0; return function () { console.log(i ...