Poj 2431 partition dition (Greedy + priority queue)

Source: Internet
Author: User
Partition dition
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6890   Accepted:2065

Description

A group of cows grabbed a truck and ventured on an unknown dition 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 too tively 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.

Sample Input

44 45 211 515 1025 10

Sample output

2

Hint

Input details:

The truck is 25 units away from the town; the truck has 10 units of fuel. along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck ). these fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

Output details:

Drive 10 units, stop to Acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

Source

Usaco 2005 u s open gold It's a simple question, but it's hard to understand the meaning of the question. I have read the question several times before I can understand the meaning. It means that a truck is far away from a town, there is still p oil in the car, each driving forward a unit distance consumes a unit of oil, if there is no oil in the way of the car, it will not be able to reach the end; there are N gas stations on the way, gas stations provide a limited amount of fuel, while trucks have unlimited fuel tanks. Each gas station is given a distance from the end point and the amount of fuel provided. Ask the trucks to add at least a few times of fuel on their way. This topic requires the greedy idea. First, we should provide a large amount of oil to store each oil volume in a priority queue, as long as the oil volume can reach the end point, the number of times added is the smallest;
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace STD; const int maxn = 10001; typedef struct node {int DIS, fuel ;} node; node stop [maxn]; bool CMP (node X, node y) // compare the size. The distance from the end point is {return X. dis> Y. DIS;} int main () {int N, L, P; int ans = 0, j = 0, temp; while (scanf ("% d", & N )! = EOF) {priority_queue <int> q; For (INT I = 0; I <n; I ++) scanf ("% d", & stop [I]. dis, & stop [I]. fuel); scanf ("% d", & L, & P); sort (Stop, Stop + N, CMP); q. push (p); // start of the oil into the queue while (L> 0 &&! Q. empty () {ans ++; temp = Q. top (); q. pop (); L-= temp; // distance that can be supported by the amount of fuel while (j <n & L <= stop [J]. dis) // compare the distance from the end to Q. push (stop [J ++]. fuel); // The amount of fuel provided by the gas station} printf ("% d \ n", l <= 0? Ans-1:-1);} return 0 ;}



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.