<title>Heap (priority queue) example</title> Heap (priority queue) example
Topic:
You need to drive a truck drive L unit distance. At the very beginning, the truck had a P-unit of petrol. The truck needs to consume 1 units of petrol per 1 unit distance. If the petrol on the way is exhausted, the truck cannot move on and thus cannot reach the end point. There were altogether n gas stations on the way. The I petrol station can give the truck plus bi unit gasoline at a distance from the beginning of the AI unit. Assuming the capacity of the truck's fuel tank is infinitely large, no matter how much oil is added. So ask the truck if it can reach the finish line? If so, how much petrol does it need to be added at least? If you can reach the end, output a minimum number of refueling, otherwise output-1.
Example:
n = 4
L = 25
p = 10
A = {10, 14, 20, 21}
B = {10, 5, 2, 4}
Output 2
// createtime:2015-04-08 13:27:16#include <iostream>#include <queue>using namespace STD;// priority_queue <int, vector<int>, greater<int> > que; Minimum heappriority_queue<int>que;// Maximum Heapint L;// the length of the roadint N;Const int MAXN= 10005;int a[MAXN];// distance of the gas station to the beginningint b[MAXN];// gas stations can add oil.int ans;int P;// How much oil did the car have at first?void Solve() {ans = 0;int POS= 0;int Tank= P;// the current oil for(int I= 0; I <= N; i++) {int D= a[i]-Pos; while(Tank < D) {// need to refuel if(Que.empty ()) {cout <<"-1"<< Endl;return; } Tank + = Que.top (); Que.pop (); ans++; } tank = tank-d;// no need to continue refueling.Que.push (B[i]); pos = A[i]; }}int Main(void) {cin >> n >> l >> p; for(int I= 0; I < n; i++) {cin >> a[i]; } for(int I= 0; I < n; i++) {cin >> b[i]; } A[n] = l; B[n] = 0; Solve (); cout << ans << endl;return0;}
Heap (priority queue) example