1. 程式人生 > >Expedition POJ 2431 (貪心與優先佇列)

Expedition POJ 2431 (貪心與優先佇列)

題面:

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, 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 effectively 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

4
4 4
5 2
11 5
15 10
25 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.

翻譯:

一群奶牛搶下一輛卡車和冒險的探險深入到叢林中。作為比較差的驅動程式,母牛不幸的是成功地執行在一個岩石和穿刺卡車的油箱,卡車目前洩漏燃油距離的每一個單元一個單元它旅行。

要修復卡車,奶牛需要開車到最近的鎮(不超過百萬單位遠)下長,蜿蜒的道路。在此道路,城鎮和卡車的當前位置之間,存在N(1 < = N <= 10,000)燃料停在奶牛可以停下來獲取額外燃料的地方(每個站點1..100個單位)。

幸運的是,他們的卡車上的油箱容量是這樣的卡車目前距離城鎮L單位,並且有P單位的燃料(1 <= P <= 1,000,000)。

確定到達城鎮所需的最小停留次數,或者奶牛根本無法到達城鎮。
輸入
*第1行:單個整數,N

*行第2..N + 1:每行包含兩個空間隔開的整數描述燃料停止:第一個整數是從鎮到停止的距離;所述第二燃料是可用在該停止的量。

*行N + 2:兩個以空格分隔的整數,L和P.
產量
*第1行:一個整數,給出到達城鎮所需的最少燃油停留次數。如果無法到達,則輸出-1。
樣本輸入
4
4 4
5 2 2
11 5
15 10
25 10
樣本輸出
2
暗示
輸入細節:

這輛卡車是10個燃料單位。沿著這條路,距離城鎮4號,5號,11號和15號的距離有4個燃料站(所以最初的距離為21,20)這些燃料停止裝置可以提供多達4個,2個,5個和10個單位的燃料。

輸出詳情:

駕駛10個單位,停止再購買10個單位,燃料再駕駛4個單位,停止再購買5個單位的燃料,然後開車到鎮上。

程式碼:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
const int maxn=10100;
#include<queue>
using namespace std;
typedef struct  
{
	int dis;
	int oil;
}s;
s stu[maxn];

bool cmp(s a,s b)
{
	return a.dis>b.dis;
}
int main()
{
	int dismax;
	int oilmax;
	int i;
	int n;
	while(cin>>n)
	 {
		memset(stu,0,sizeof(stu));
		priority_queue<int>q;
		for(i=0;i<n;i++)
			{
				cin>>stu[i].dis>>stu[i].oil;
			}
			
		cin>>dismax>>oilmax;
		
	    sort(stu,stu+n,cmp);
	    
		q.push(oilmax);
		
		int res=0;i=0;
		
		while(dismax>0&&!q.empty())
		{
			res++;
			dismax-=q.top();
				q.pop();
			while(dismax<=stu[i].dis&&i<n)
			{
				q.push(stu[i++].oil);	
			}		
		}
		if(dismax<=0)
			cout<<res-1<<endl;
		else
			cout<<"-1"<<endl;
	}
	
	return 0;
} 

有個坑點 ,是多組輸入。

優先佇列與貪心 的題目,看車 能走的最遠距離,現將油站的距離排序,有油時,將車站 的油數量進優先佇列,沒油時,看看 哪個 油站的油最多,拿多的那個,出佇列,再開到城鎮。 

現將 現有油 能開到的所有範圍內的油田的油數量 進佇列,當沒油時 ,挑選所途經的油數量最多的 油站進行加油,貪心策略是在達到最遠的情況下,挑選 油量 最多的 油站 。