PTA(Advanced Level)1033.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: 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的更多相关文章
- 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 ...
- PAT (Advanced Level) 1033. To Fill or Not to Fill (25)
贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1003 Emergency
Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA (Advanced Level) 1018 Public Bike Management
Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 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 ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
随机推荐
- learning express step(十二)
learning express view engine function const express = require('express'); const app = express(); app ...
- 文件描述符、文件表项、V节点表项的一些总结
转自 http://blog.csdn.net/gzzheyi/article/details/7739556 表格可以参见APUE 第三版 P61. 文件描述符(进程级别): 1).在每个进程表中 ...
- 修改history记录数
在linux系统下.history命令会保存多少条命令呢?曾在一本书上说,如果注销系统,那么会将所有的历史命令都定入到~/.bash_history, 但只保留1000条命令(这个是由默认的shell ...
- jquer属性 offset、position、scrollTop
尺寸操作 1.获取宽高 a) jq对象.height/width () :只有获取高度/宽度 尺寸,不包括padding和margin 和 border 2.设置宽度 ...
- call()与构造函数的运用
一.简介 call()和apply()方法是所有函数体的固有属性,可以在指定作用域下调用函数.这里面有两层意思:1.可以在另外的作用域下调用函数:2.当函数体是是构造函数时,call()方法能达到类之 ...
- (main)贝叶斯统计 | 贝叶斯定理 | 贝叶斯推断 | 贝叶斯线性回归 | Bayes' Theorem
2019年08月31日更新 看了一篇发在NM上的文章才又明白了贝叶斯方法的重要性和普适性,结合目前最火的DL,会有意想不到的结果. 目前一些最直觉性的理解: 概率的核心就是可能性空间一定,三体世界不会 ...
- 移动端—— 兼容PC端,移动端的点击事件
移动设备上不支持鼠标事件,好在webkit内核的移动浏览器支持 touch 事件,所以触摸事件是移动应用中所必须的.touchstart.touchmove.touchend事件可以类比于moused ...
- LevelDB深入浅出之整体架构
LevelDB是一个可持久化的KV数据库引擎,由Google传奇工程师Jeff Dean和Sanjay Ghemawat开发并开源.无论从设计还是代码上都可以用精致优雅来形容,非常值得细细品味.本文将 ...
- 通过id获取指定元素内容(标签里面的 标签内容获取)
html页面如下 <tr style="background-color:#fff;"> <td colspan="2" align=left ...
- python3 高级编程(一) 使用__slots__
使用__slots__的目的:限制实例的属性 用法:定义class的时候,定义一个特殊的__solts__变量,来限制实例能添加的属性. class Student(object): __slots_ ...