Expedition
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19609 | Accepted: 5653 |
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.
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
* 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.
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.
Source
给你车辆最开始距离目的地的距离和油箱内的初始燃油,告诉你路上有N个可以补充燃油的地点,每个地点距离目的地的距离和可以补给燃油的数量,问是否能到达目的地,若能,询问最小需要多少次加油,若不能,输出-1。
这个问题换一个思路就很好解决。我们每到达一个加油站,我们就有选择加不加油的权力,我们可以一直跑到没油,然后我们再选择以及通过的加油站当中【即有加油权力的加油站当中】选择油最多的一个加油就好,即假装路过那个加油站的时候加了油。
按照这个思路,那么我们可以创建一个优先队列,里面存储每个加油点的油量,每通过一个加油点我们就把这个加油点的油量储存到这个优先队列就好,当油箱没有油的时候,我们每次只要push取出油量最多的一个加油点的油去加就好,如果队列当中一个可以加油的加油点都没有,那我们就可以认为无法到达终点。
注意数据不一定排过序,为此WA了一发。
附上AC代码。
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct{
int jour,oil;
}Poi;
Poi poi[10006];
bool cmp(Poi a,Poi b){
if(a.jour<b.jour) return true;
else return false;
}
/*看上面那一堆基础极其不过关的写法。。编程功底还是太差*/
void solve(void)
{
int n;
while(~scanf("%d",&n)){
for(int i = 0 ; i < n ; i++) scanf("%d%d",&poi[i].jour,&poi[i].oil);
sort(poi,poi+n,cmp);//加油点的顺序不是按顺序给的,要先排一次序。
int myjour,tank; scanf("%d%d",&myjour,&tank);
priority_queue<int>q;
int flag = n-1,ans = 0;
for(int i = myjour ; i>=0 ; i--,tank--){
//通过了一个加油站,把这个加油点的油量加入优先队列当中。
if(poi[flag].jour==i){
q.push(poi[flag].oil);
flag--;
}
//到达了终点
if(i==0) printf("%d\n",ans);
if(tank==0){
if(q.empty()){//可以加油的地方也没有了。
printf("-1\n");
break;
}
else{
tank+=q.top();//加一次最多的。
q.pop();
ans++;
}
}
}
}
}
int main(void)
{
solve();
return 0;
}