【贪心算法】poj 2431: Expedition(最优加油方法)

题目描述(传送门

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck’s fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1…100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input

  • Line 1: A single integer, N

  • Lines 2…N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

  • Line N+2: Two space-separated integers, L and P

Output

  • Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

Hint

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

题目翻译

已知一条公路上,有一个起点和一个终点,这之间有n个加油站;已知从这n个加油站到终点的距离d与各个加油站可以加油的量l,起点位置至终点的距离为L,起点时刻油箱中汽油量P;假设使用一个单位的汽油即走一个单位距离,油箱没有上线,最少加几次油,可以从起点开至终点???(如果无法到达,返回-1)

思考

在这里插入图片描述
汽车经过n个加油站,对于这n个加油站,应该在那个加油站停下来加油,最终既能到达终点,又使得加油次数最少?

若按照顺序遍历加油站,则面临:

  • 如果在某个加油站停下来加油,可能是没必要的,有可能不进行这次加油也能到达终点;
  • 如果在某个加油站不停下来加油,可能由于汽油不够而无法到达终点或者后面要更多次的加油才能到达终点。

这道题有点像:【贪心算法】leetcode45. 跳跃游戏II
要考虑神魔时候加或者不加,应选取能到达距离之内的一个最大值。

贪心规律

何时加油最合适??
油用光的时候加油最合适!!
在那个加油站加油最合适??
在油量最多的加油站加油最合适!!

在这里插入图片描述

算法思路

  • 设置一个最大堆,用来存储经过的加油站的汽油量
  • 按照从起点至终点的方向,遍历各个加油站之间与加油站到终点距离
  • 每次需要走两个加油站之间的距离d,如果发现汽油不够走距离d时,从最大堆取出一个汽油量添加,直到可以足够走距离d
  • 如果把最大堆的汽油量都添加仍然不够行进距离d,则无法到达终点
  • 当前油量P减少d
  • 将当前加油站油量添加至最大堆

在这里插入图片描述

代码

package com.wenhui.lession3;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @ClassName poj2431
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/02/05/20:14
 */
public class poj2431 {
    // L :起点到终点的距离
    // P :起点初始汽油量
    // stop[i][0] 加油站到终点的距离
    // stop[i][1] 加油站汽油量
    public static int get_mininum_stop(int L, int P, int[][] stop) {

        // 存储汽油量的最大堆
        Queue<Integer> big_queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
        int result = 0; // 记录加过几次油
        Arrays.sort(stop, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[0] - o1[0];
            }
        });
        //Arrays.sort(stop, ((o1, o2) -> o1[0] - o2[0]));// 以停靠点至终点距离从大到小排序
        // 遍历每一个停靠点
        for (int i = 0; i < stop.length; i++) {
            int dis = L - stop[i][0];
            while (!big_queue.isEmpty() && P < dis ) {
                P += big_queue.poll();
                result++;
            }
            if (big_queue.isEmpty() && P < dis) {
                return -1;
            }
            P = P - dis;
            L = stop[i][0];
            big_queue.offer(stop[i][1]);
        }
        return result;
    }

    public static void main(String[] args) {

        int L = 25;
        int P = 10;
        // 终点(0,0)加进去
        int[][] stop = new int[][]{{4, 4}, {5, 2}, {11, 5}, {15, 10}};//,{0,0}};
        System.out.println(get_mininum_stop(L, P, stop));

    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值