[PAT]1033. To Fill or Not to Fill (25)(Java实现)
来源:互联网 发布:矩阵乘法怎么算 编辑:程序博客网 时间:2024/06/07 13:53
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
package go.jacob.day1129;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Scanner;/* * 处在当前加油站(起点加油站)的情况下 * 情况1:600米范围内,有目的地——计算恰好能到目的地的油量 * 情况2:600米范围内没有加油站,无论油价多贵——加满——能跑多远算多远 * 情况3:600米范围内有加油站: * a:有比当前加油站的油价更便宜的加油站——加到恰好能到那个最近的油站的油量 * (注:1、如果有多个便宜的,还是要先选取最近的那个,而不是最便宜的那个;2、可能还有油剩余) * b:没有比当前加油站的油价更便宜的加油站——加满,然后在600范围内找到最便宜的加油站加油 *//** * 参考解法:https://www.cnblogs.com/XBWer/p/3866486.html * * @author Administrator 贪心算法求解 */public class Demo2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);double tank = sc.nextDouble(), distance = sc.nextDouble(), avgDist = sc.nextDouble();int n = sc.nextInt();List<Station> list = new ArrayList<Station>();for (int i = 0; i < n; i++) {Station s = new Station(sc.nextDouble(), sc.nextInt());list.add(s);}sc.close();// 对Station的位置进行排序Collections.sort(list);// 特殊情况:1.目的地址为原点;2.原点没有加油站if (distance == 0) {System.out.println("0.00");return;}if (list.get(0).getDist() != 0) {System.out.println("The maximum travel distance = 0.00");return;}int curStNum = 0;// 当前所处加油站编号double curGas = 0;// 当前的油量double curCost = 0;// 当前花费boolean flag = false;// 是否可以到达目的地double maxDis = tank * avgDist;while (!flag) {boolean tag = false;// 最大距离内是否有加油站boolean ifCheaper = false;// 是否有便宜的double cheapestPrice = 10000;// 找出最便宜的int cheapestNum = 0;// 没有更便宜的情况下,找出最便宜的for (int i = curStNum + 1; i < n; i++) {// 在可达范围内有加油站if ((list.get(i).getDist() - list.get(curStNum).getDist()) <= maxDis) {tag = true;// 有加油站if (list.get(i).getPrice() < list.get(curStNum).getPrice()) {// 情况3-a:在可达范围内有比当前更便宜的加油站ifCheaper = true;double dist = list.get(i).getDist() - list.get(curStNum).getDist();double needGas = dist / avgDist - curGas;curGas = 0;curCost += needGas * list.get(curStNum).getPrice();curStNum = i;break;}if (list.get(i).getPrice() < cheapestPrice) {cheapestPrice = list.get(i).getPrice();cheapestNum = i;}} else// 范围内没有加油站break;}// (没有更便宜的加油站)说明已经可以到达目的地了,情况1if (!ifCheaper && (distance - list.get(curStNum).getDist() <= maxDis)) {double dist = distance - list.get(curStNum).getDist();double needGas = dist / avgDist - curGas;curCost += needGas * list.get(curStNum).getPrice();System.out.printf("%.2f", curCost);return;}if (tag && !ifCheaper) {// 情况3-b:没有比当前加油站的油价更便宜的加油站——加满,然后在600范围内找到最便宜的加油站加油double needGas = tank - curGas;curCost += (needGas * list.get(curStNum).getPrice());double dist = list.get(cheapestNum).getDist() - list.get(curStNum).getDist();curGas = tank - dist / avgDist;curStNum = cheapestNum;} else if (!tag) {System.out.print("The maximum travel distance = ");System.out.printf("%.2f", list.get(curStNum).getDist() + maxDis);return;}}}/* * 加油站类 */static class Station implements Comparable<Station> {private double price;private double dist;public Station(double price, double dist) {super();this.price = price;this.dist = dist;}@Overridepublic int compareTo(Station s1) {return this.dist - s1.getDist() < 0 ? -1 : 1;}public double getPrice() {return price;}public double getDist() {return dist;}}}
- [PAT]1033. To Fill or Not to Fill (25)(Java实现)
- 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 A 1033. To Fill or Not to Fill (25)
- 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 (25)
- PAT甲级.1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25) PAT 甲级
- PAT甲级1033. To Fill or Not to Fill (25)
- 2017-11-25前端初识协会分享会
- 自定义注解,仿butterknife,找控件点击事件
- <01读书回忆录
- C#构造函数里的base和this的区别
- 网络接口,与网络桥接的管理。
- [PAT]1033. To Fill or Not to Fill (25)(Java实现)
- MOOC微软课程:深度神经网络在语音识别上的应用
- 88. Merge Sorted Array 合并有序数组
- 2017传智播客python基础班+就业班
- ubuntu 下出现E: Sub-process /usr/bin/dpkg returned an error code
- 【Scikit-Learn 中文文档】内核近似
- 使用Python和xlwt向Excel文件中写入中文
- Tomcat集群实现Session共享
- 'parent.relativePath' of POM spring.boot.core: