课程:F. 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.

题意:

  题目意思是一辆卡车距离城镇有L距离,车中还有P油量,每向前行驶一单位距离消耗一单位的油量,如果在途中卡车中就没有油了 ,就无法到达终点;途中有N个加油站,加油站提供的油量有限,卡车的油箱是无限的,给出每个加油站距离终点的距离和提供的油量,问卡车在途中最少需要加几次油。

解题思路:

  采用贪心的思想,卡车当然在不加油的情况下走的越远越好了,而当它没油时,我们再判断卡车在经过的途中的加油站,哪个加油站加的油最多,选油量最多的,这样后面加油次数也越少,然后又继续行驶,当它又没油了的时候,继续选它从起点到该点所经过的加油站油量最多的加油。
  做法先将加油站到终点的距离由远到近排下序,这样离起点就是由近到远。就是每经过一个加油站就将该加油站的油量压入优先队列中,然后每次没油的时候,去队首元素加油即可。
•在经过加油站i时,往优先队列里加入Bi。
•当燃料箱空了时,
  如果优先队列也是空的,则无法到达终点。
  否则取出优先队列中的最大元素,并哟过来给卡车加油。

题解:

#include
#include
#include
#include 
using namespace std;
#define MAX 10001
//定义每个加油站的结构体 
struct node            
{
    int dis,flue;
}stop[MAX];
//将每个加油站按距出发点的距离由小到大排序 
bool cmp(node a,node b)     
{
    return a.dis>b.dis;
}
int main()
{
    int N,L,P;
    while(cin>>N)
    {
        //输入
        for(int n=0;n> stop[n].dis >> stop[n].flue;         
        cin >> L >> P;
        //将终点视作dis=0,flue=0的加油站 
        stop[N].dis = 0;
        stop[N].flue = 0;
        sort(stop,stop+N,cmp);
        N++;
        //维护优先队列 
        priority_queue que;
        //ans: 加油次数,pos:当前位置,tank:剩余油量 
        int ans = 0,pos = L,tank = P;
        //i是下个加油站的索引 
        for(int i=0;i

你可能感兴趣的:(课程:F. Expedition)