Heap (priority queue) example

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.