Expedition
Time Limit: 1000msMemory Limit: 65536KB This problem will be judged on PKU. Original ID: 2431
64-bit integer IO format: %lld Java class name: Main
Prev Submit Status Statistics Next
Type:
None
Tag it!
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.
优先队列水题
处理起来还是很py的
由于N比较大,应该找一个高效的解法。稍微转换一下思考方式:在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一次在之后的任何时候都可以加Bi单位汽油的权利”,在解决问题上也是一样的。而在之后需要加油时,就认为是在之前经过的加油站加的油就可以了。因为希望加油次数尽可能少,所以当燃料为0了之后再加油是最好的选择。基于贪心的思想,当燃料为0时,选择能加油量最大的加油站。所以可以用一个优先队列来保存经过的加油站的油量,当需要加油时,取出队列中的最大元素即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
using namespace std;
const int MAX = 999999;
const double Eps = 1e-12;
const double PI = acos(-1.0);
int gcd(int x, int y)
{
return x%y == 0 ? y : gcd(y, x%y);
}
struct node
{
int dis;
int fuel;
}a[10005];
bool cmp(node a, node b)
{
return a.dis < b.dis;
}
int main()
{
int n;
while (cin>>n)
{
int L, P;
for (int i = 0; i < n; i++)
cin >> a[i].dis >> a[i].fuel;
cin >> L >> P;
for (int i = 0; i < n; i++)
a[i].dis = L - a[i].dis;
a[n].dis = L;
a[n].fuel = 0;
n++;
sort(a, a + n, cmp);
priority_queue<int> que;
int ans = 0, pos = 0,tank=P;
for (int i = 0; i <n; i++)
{
int d = a[i].dis-pos;
while (tank < d)
{
if (que.empty())
{
puts("-1");
return 0;
}
tank += que.top();
que.pop();
ans++;
}
tank -= d;
pos = a[i].dis;
que.push(a[i].fuel);
}
cout << ans << endl;
}
return 0;
}