主要是运用贪心算法,还有一个点没过,不知道是什么问题
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
/*
* 贪心算法问题
*
* 1.当起点没有加油站的时候,即所给的加油站站点,没有distance=0的,直接输出最远的距离为0,然后结束程序。
2.当车每到一个站点,看在这个车满油的情况下所能够到达的加油站点,选择距离最近且价格低于当前站点的加油站进行前往
3.如果在满油情况下可以到达的加油站点的油价均高于当前站点,就在当前站点加满油,然后前往这些站点中价格中最低的,
这样可以保证我所走的每一段路都是价格尽可能的低
4.如果在满油的情况下,一个加油站都不能到达,那么跳出循环,输出最远能到达的距离,
即当前加油站的distance+满油情况下可以跑的距离。
*/
public class pta_1033 {
static Double maxTravel, Tardist;
static class gasStation implements Comparable<gasStation>{
Double price;
Double dis;
public gasStation(Double price, Double dis) {
this.price = price;
this.dis = dis;
}
@Override
public int compareTo(gasStation o) {
return this.dis < o.dis ? -1 : 1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] in = bf.readLine().split(" ");
Double max = Double.parseDouble(in[0]); // 最大容量
Tardist = Double.parseDouble(in[1]); // 目标距离
Double d = Double.parseDouble(in[2]); // 每单位容量行驶距离
int n = Integer.parseInt(in[3]); // 加油站数量
maxTravel = max * d; // 最大距离
ArrayList<gasStation> road = new ArrayList<>();
for(int i = 0; i < n; i++) {
in = bf.readLine().split(" ");
Double place = Double.parseDouble(in[1]);
Double price = Double.parseDouble(in[0]);
road.add(new gasStation(price, place));
}
Collections.sort(road);
Double res = 0.0;
if(Tardist == 0) {
System.out.println("0.00");
return;
}
if(road.get(0).dis != 0) {
System.out.println("The maximum travel distance = 0.00");
return;
}
int currStation = 0;
Double resDist = 0.0;
Double gasCapacity = 0.0;
boolean flag = false;
while(true) {
gasStation cur = road.get(currStation);
Double minPrice = cur.price;
int cheaperIndex = -1;
int index = -1;
Double secondMin = 10000.0;
Double currDis = cur.dis;
for(int i = currStation + 1; i < n; i++) {
Double price = road.get(i).price;
if(road.get(i).dis > currDis + maxTravel) {
break;
}
if(price >= minPrice && price < secondMin) {
secondMin = price;
index = i;
continue;
}
if(price < minPrice) {
minPrice = price;
cheaperIndex = i;
break;
}
}
// 找到在最大行驶公里内比所在加油站油价更低的加油站
if(cheaperIndex != -1) {
gasStation g = road.get(cheaperIndex);
Double targetGas = (g.dis-cur.dis)/d;
if(targetGas <= gasCapacity) {
gasCapacity -= targetGas;
}else {
res += (targetGas - gasCapacity)*cur.price;
gasCapacity = 0.0;
}
currStation = cheaperIndex;
continue;
// 找到在最大行驶公里内比所在加油站油价高但第二低的加油站
}else if(index != -1) {
gasStation g = road.get(index);
Double subCap = max - gasCapacity;
res += subCap * cur.price;
gasCapacity = max - (g.dis-cur.dis)/d;
currStation = index;
}
// 最大行驶距离内没有加油站
else {
resDist = cur.dis + maxTravel;
// 能够到达目的地
if(resDist >= Tardist) {
flag = true;
Double targetGas = (Tardist-cur.dis)/d;
if(targetGas > gasCapacity) {
targetGas -= gasCapacity;
}
res += targetGas * cur.price;
}
// 未能到达目的地
else {
}
break;
}
}
if(flag) {
System.out.printf("%.02f",res);
}else {
System.out.printf("The maximum travel distance = %.02f" , resDist);
}
}
}