首页 > 试题广场 >

To Fill or Not to Fill

[编程题]To Fill or Not to Fill
  • 热度指数:9150 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例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
50 1300 12 2
7.10 0
7.00 600

输出

749.17
The maximum travel distance = 1200.00
头像 绫清竹
发表于 2021-02-06 00:47:37
贪心算法,先按油费对每个加油站进行排序,再从低油费的开始,让其走完最长的距离(即题中的cmax*davg),设置标志位 tag[] 记录是否走了。若当前油站可走的路有些已被其他油站走了,则跳过,对所有加油站都记录可走的路程。最后检验tag[] 是否有false,若有则说明不能走完整段路程,若全为tr 展开全文
头像 健康快乐最重要
发表于 2020-03-24 12:39:27
一个题折磨了我三个小时。。。思路:贪心:1.起步时,如果没有距离是0的加油站,肯定走不了2.在每个加油站都判断在最大油箱行驶距离内是否有比当前油价更便宜的2.1如果没有,则加满(如果能直接到终点就加到终点的油就行了,这样比较便宜)。并且判断是否两个加油站的距离大于油箱的最大行驶距离,如果是直接输出; 展开全文
头像 不红红黑路同
发表于 2022-02-14 09:56:06
设置大小为d(总距离)的flag数组,初始值为-1,表示每个地方都不可达 按照价格从低到高的顺序遍历每个加油站,对每个加油站,计算它加满油的情况下能覆盖哪些地方。例如,容量为Cmax,每单位汽油可以跑的距离为Davg,当前遍历到的加油站位置为Di,价格为Pi——则遍历flag数组,把所有flag数组 展开全文
头像 糖分椰蓉糯米糍
发表于 2022-02-09 11:49:48
贪心:目标是花费最少,就从“油价”开始“贪”。 将加油站按油价升序排列,依次遍历, 让油价低的加油站出发走尽可能多的路,即走Cmax*Davg,走过的路在路程数组上标记一下, 若从一个加油站出发到最大距离之间有部分已经被走过,则去掉,剩下这段路按该加油站油价收费。 最后若路程数组还有未标记的路段,即 展开全文
头像 zoey447
发表于 2022-02-09 20:43:26
要点:多用便宜油,贵的油能少用就少用 先将站点按离杭州(起点)的距离排序(距离相同时价格低的优先) 从第一个站点开始,若第一个站点距离不为0,说明起点没有加油站,跑不动,返回能跑的最远距离0 否则进行循环,循环结束的条件:1.当前位置是最后一个站点 2.无法到达下一站点 3.当前站点加满油可达终点且 展开全文
头像 EcnuOnion
发表于 2020-05-12 21:04:28
本题解思路来源于其他人的题解,在他的基础上进行了优化。先提供一份他人的题解,我将其翻译成了java语言版本。 import java.util.*; public class Main { public static void main(String[] args) { Sc 展开全文
头像 Sun_Wu_Kong
发表于 2024-06-20 13:27:33
核心思路就是一句话:让最低的油价尽量跑的最远。 先对加油站按油价升序排序,让油价低的排前面。 核心代码如下: for (int i = 0; i < n; i++) { int station_distance = 0; //记录第i个加油站加的油的行驶距离 int dis = 0; 展开全文
头像 土尔逊Torson
发表于 2023-06-01 00:56:25
//土尔逊Torson 编写于2023/06/01 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cstdio> #include <st 展开全文
头像 苇岸弦歌
发表于 2023-02-23 10:44:15
区间贪心算法,后一个区间的起点较前一区间偏移多少,区间终点也会偏移多少,所以不用考虑油箱容积的变化。另外浮点数需使用double,float不能通过测试。 #include <iostream> #include <algorithm> using namespace std 展开全文
头像 大内高手
发表于 2020-04-05 11:09:41
此题考察的是贪心算法。 解题思路: 在起点加油,加多少不确定。记录当前油价,从下一站开始查找,满足条件 下一站的dis <= (当前dis + 加满油最多的距离),如果遇到此站的油价小于当前油价,则从上一站加油只需要加到能够跑到此站为止即可,若满足条件的站点的油价都大于上一次当前站的油价,那么 展开全文