PAT (Advanced Level) 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: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; andN(≤500), the total number of gas stations. ThenNlines follow, each contains a pair of non-negative numbers:Pi, the unit gas price, andDi(≤D), the distance between this station and Hangzhou, fori=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, printThe maximum travel distance = X
whereX
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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.DecimalFormat; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); String[] s1 = s.split(" "); double C_max = Double.parseDouble(s1[0]); //油箱最大容量 double D = Double.parseDouble(s1[1]); //目的地距離 double D_avg = Double.parseDouble(s1[2]); //每單位的油可行使的距離 Integer N = Integer.parseInt(s1[3]); //加油站總數 ArrayList<double[]> gasStations = new ArrayList<>(); for (int i = 0; i < N; i++) { String s2 = reader.readLine(); String[] s3 = s2.split(" "); double[] station = new double[2]; station[0] = Double.parseDouble(s3[0]); station[1] = Double.parseDouble(s3[1]); gasStations.add(station); } double[] station = new double[2]; station[0] = 0; station[1] = D; gasStations.add(station); GetMinPrice(gasStations, C_max, D, D_avg); } public static void GetMinPrice(ArrayList<double[]> gasStations, double C_max, double D, double D_avg){ DecimalFormat decimalFormat = new DecimalFormat("0.00"); int N = gasStations.size(); Collections.sort(gasStations, new Comparator<double[]>() { @Override public int compare(double[] o1, double[] o2) { if (o1[1] > o2[1]){ return 1; }else { return -1; } } }); double curDis = 0, curPrice = 0, totalPrice = 0, leftDis = 0; if (gasStations.get(0)[1] != 0){ System.out.println("The maximum travel distance = 0.00"); return; }else { curPrice = gasStations.get(0)[0]; } while (curDis < D){ double maxDis = curDis + C_max * D_avg; //當前加油站加滿油能到到的最遠位置 double minPrice = Double.MAX_VALUE, minPriceDis = 0; boolean flag = false; for (int i = 0; i < N && gasStations.get(i)[1] <= maxDis; i++){ if (gasStations.get(i)[1] <= curDis) continue; if (gasStations.get(i)[0] < curPrice){ totalPrice += (gasStations.get(i)[1] - curDis - leftDis) * curPrice / D_avg; leftDis = 0; curDis = gasStations.get(i)[1]; curPrice = gasStations.get(i)[0]; flag = true; break; } if (gasStations.get(i)[0] < minPrice){ minPrice = gasStations.get(i)[0]; minPriceDis = gasStations.get(i)[1]; } } if (!flag && minPrice != Double.MAX_VALUE){ totalPrice += curPrice * (C_max - leftDis / D_avg); leftDis = C_max * D_avg - (minPriceDis - curDis); curPrice = minPrice; curDis = minPriceDis; } if (!flag && minPrice == Double.MAX_VALUE){ curDis += C_max * D_avg; System.out.println("The maximum travel distance = " + decimalFormat.format(curDis)); return; } } System.out.println(decimalFormat.format(totalPrice)); } }
本文來自部落格園,作者:凸雲,轉載請註明原文連結:https://www.cnblogs.com/jasonXY/p/15178047.html