优先队列+贪心

2018-06-17 21:32:01来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

https://vjudge.net/problem/POJ-2431

题目:

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的路程,路上有n个加油站,车从起点出发,在起点时,车上有汽油容量为p,已知单位距离1,就会消耗汽油1,已知n个加油站距离终点的距离和各个加油站可以加的油量,然后问最少需要加多少次油可以到达终点,若不能到达终点,输出“-1”。

 

解析:优先队列:

当经过加油站的时候就入队

当燃料箱空的时候:

    1,如果优先队列是空的,则不能到达,输出-1

   2,若不为空,则取出优先队列的最大油量,并用来加油。

贪心思想的话就是每次取出最大油量就能使加油的次数最少

 

ps:需要排序

,    题目给出的距离是只距离终点距离,需要处理成距离起点的距离

 

 代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 10005
using namespace std;
//  x表示城镇,y为油量
struct cmp2{
     bool operator () (int &a,int &b){
         return a<b; //最大值优先
     }
};
struct zz{
 int x;
 int y;
}z[maxn];
bool cmp(zz a,zz b)
{
    if(a.x==b.x)return a.y>b.y;
    return a.x<b.x;
}
int main()
{
    int n,l,q;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>z[i].x>>z[i].y;
        }
        cin>>l>>q;
        for(int i=0;i<n;i++)
        {
            z[i].x=l-z[i].x;
        }
        sort(z,z+n,cmp);
         priority_queue<int,vector<int>,cmp2>que; //最大值优先
       z[n].x=l;
       z[n].y=0;
       n++;
       int ans=0,pos=0,tank=q;
       for(int i=0;i<n;i++)
       {
           int d=z[i].x-pos;
           while(tank<d)
           {
               if(que.empty())
               {
                   cout<<"-1"<<endl;
                   return 0;
               }
               else
               {
                   tank=tank+que.top();
                   que.pop();
                   ans++;
               }
           }
           tank=tank-d;
           pos=z[i].x;
           que.push(z[i].y);
       }
       cout<<ans<<endl;
    }
    return 0;
}

这题WA了8次,,心态爆炸啊,反复改,看别人代码查不同,,最后都快改成一样的才过的

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:字符串变量的定义与引用

下一篇:洛谷P2234 [HNOI2002]营业额统计