POJ 2431 Expedition (STL priority queue)

Source: Internet
Author: User


Expedition
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8053 Accepted: 2359

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, Windin G Road. On this road, between the town and the current location of the truck, there is n (1 <= n <=) fuel stops wher E The cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and was especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the the-the-the-town. Fortunately, the capacity of the fuel tank on their truck are so large that there are effectively no limit to the amount of Fuel it can hold. The truck is currently L units away from the town and have 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, 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 isn't possible to reach of the town, Output-1.

Sample Input

44 45 211 515 1025 10

Sample Output

2

Hint

INPUT DETAILS:

The truck is a units away from the town; The truck has ten units of fuel. Along the road, there is 4 fuel stops at distances 4, 5, one, and (so these is initially at distances 21 , +, + from the truck). These fuel stops can supply up to 4, 2, 5, and ten units of fuel, respectively.

OUTPUT DETAILS:

Drive ten units, stop to acquire ten more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then driv E to the town.

Source

Usaco 2005 U S Open Gold


Topic Links:

id=2431 ">http://poj.org/problem?id=2431

Title: There are n gas stations, a truck starts at a distance of L and has a P-liter oil from its destination. Each gas station has two values, distance from the destination and the amount of oil, now ask the truck from the beginning to the end of the minimum to add a few times, each refueling feel is to add gas station oil complete the

Topic Analysis: First of all to pay attention to the distance of the gas station is the relative destination, we want to convert it to the distance from the We calculate the amount of fuel needed for the truck from the current point to the next. If not enough in the previous gas station, and in order to make the least number of refueling, it is obvious that the largest oil filling stations to add first. This selects the data structure of the subject priority queue, each to a petrol station. Put it in the queue and fill it up directly from the queue. The queue is empty when the description is unreachable, note that the input of this question is not necessarily in the order of distance, so you need to sort the sequence

#include <cstdio> #include <algorithm> #include <queue>using namespace std;int Const MAX = 1e4 + 5;struct stop{int dis, fuel;} S[max];bool CMP (Stop A, stop B) {return A.dis < B.dis;}    int main () {int n, l, p;    scanf ("%d", &n);    for (int i = 0; i < n; i++) scanf ("%d%d", &s[i].dis, &s[i].fuel);    scanf ("%d%d", &l, &p);    for (int i = 0; i < n; i++) S[i].dis = L-s[i].dis;  S[n].fuel = 0;    Add the end point in S[n++].dis = L;    Sort (s, S + N, CMP);    Priority_queue <int> Q;    int ans = 0, pos = 0, num = p;        for (int i = 0; i < n; i++) {int d = s[i].dis-pos;                while (Num < D) {if (Q.empty ()) {printf (" -1\n");            return 0;     } ans + +;            Refueling num + = Q.top ();        Q.pop ();           } num-= D;        Q.push (S[i].fuel);    pos = S[i].dis; } printf ("%d\n", ans);}


POJ 2431 Expedition (STL priority queue)

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.