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;
} 

有个坑点 ,是多组输入。

优先队列与贪心 的题目,看车 能走的最远距离,现将油站的距离排序,有油时,将车站 的油数量进优先队列,没油时,看看 哪个 油站的油最多,拿多的那个,出队列,再开到城镇。 

现将 现有油 能开到的所有范围内的油田的油数量 进队列,当没油时 ,挑选所途经的油数量最多的 油站进行加油,贪心策略是在达到最远的情况下,挑选 油量 最多的 油站 。

猜你喜欢

转载自blog.csdn.net/tsam123/article/details/85108235