POJ 2431 Expedition (greedy + priority queue)

Source: Internet
Author: User

Expedition
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7696 Accepted: 2260

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



Test instructions: To the car to travel distance L, the road has N gas station, the car from the starting point, at the starting point, the car has a gasoline capacity of P, known unit distance of 1, will consume gasoline 1, known as N gas station distance from the end of the distance and the gas stations can add the amount of oil, and then ask at least how many times If you cannot reach the end point, output "-1".


Analysis: According to the greedy thought, we must be as far as possible to use up the oil and then add, but each gas station refueling amount there is a difference, so you can not simply greedy. We can see, because do not know when to add the most suitable, so we can first put those through the gas station in the priority queue, to the time when the need to add, the equivalent of the station when the oil was added, the effect is the same. This will be possible, each add is the largest oil, so the last refueling times is the least.




AC Code:

#include <cstdio> #include <queue> #include <iostream> #include <algorithm>using namespace std;        #define MAXN 10002int A[MAXN], b[maxn];struct node{int A; int b;}; Gas station structure, position A, oil volume BNode M[maxn];bool CMP (node x, node Y) {////define the sort function by itself, first ascending by position, and then by the amount of oil descending if (x.a = = y.a) return x.    b > y.b; return x.a < y.a;}    int main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int N, l, p;        while (scanf ("%d", &n)!=eof) {for (int i=0; i<n; i++) scanf ("%d%d", &m[i].a, &m[i].b);        scanf ("%d%d", &l, &p);          for (int i=0; i<n; i++) m[i].a = l-m[i].a;               Processes the data that is read in, converting the position to the location of the starting point m[n].a = l;        For convenience, the end is also regarded as a petrol station m[n].b = 0;        n + +;                  Sort (m, m+n, CMP);        Gas stations sorted by location priority_queue<int> Q;        int flag = 1;     int ans = 0, pos = 0, tank = p; Number of refueling ans, the position of the car pos, the remaining oil on the vehicle tank for (int i=0; i<n; i++) {int d =M[i].a-pos;              The next distance to travel while (tank < D) {//continue to refuel until the oil volume is sufficient to travel to the next gas station if (Q.empty ()) {                    No petrol at the gas station, the rep can't complete the mission. Flag = 0;                Break                } Tank + = Q.top ();                Q.pop ();            Ans + +;            } if (!flag) break;            Tank-= D;            pos = m[i].a;                 Q.push (M[I].B);    Put the amount of fuel passing through the gas station into the priority queue} printf ("%d\n", flag ans:-1); } return 0;}


PS: Priority Queue (priority_queue) is amazing, very easy to use, internal heap implementation, so time complexity is still excellent ~ ~ ~



POJ 2431 Expedition (greedy + 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.