优先队列+贪心

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.

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.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.

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.

题意:
一群牛抓住一辆卡车,冒险到丛林深处探险。作为一个差劲的司机,这些奶牛不幸地从岩石上碾过,刺破了卡车的油箱。卡车现在每行驶一单位距离就泄漏一单位燃油。
为了修理卡车,奶牛们需要沿着一条长长的蜿蜒的道路行驶到最近的城镇(距离不超过1000000个单位)。在这条道路上,在城镇和卡车的当前位置之间,有N个(1<=N<=10000)燃料站,奶牛可以停下来获取额外的燃料(每个站1…100单位)。
丛林对人类来说是一个危险的地方,对奶牛来说尤其危险。因此,奶牛们希望在去镇上的路上尽可能少地停下来加油。幸运的是,他们卡车上的油箱容量如此之大,实际上它所能容纳的燃油量是没有限制的。卡车目前距离城镇1个单位,燃料为P个单位(1<=P<=1000000)。
确定到达城镇所需的最小停车次数,或者如果奶牛根本无法到达城镇。

思路:将数据变成距离起点距离,从近到远排序;行驶中到加油站时油量大于等于0时入队,继续行驶;如果小于0,将油量从大到小加上记入队列的油,再出队,加油次数加1;队列为空仍不能到达下一个加油站,结束;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,x,y;
struct mm
{
	int a,b;
} p[200005];
bool ac(mm a,mm b)
{
	return a.a<b.a;
}
void solve()
{
	p[n].a=x;
	p[n++].b=0;
	priority_queue<int>q;//优先队列 (自动从大到小排序); 
	int fank=y,pos=0,ans=0;//pos(距离起点距离) ;fank(油量) 
	for(int i=0;i<n;i++)
	{
		int d=p[i].a-pos;//d:两个加油站 的距离; 
		while(fank-d<0)//无法到达 
		{
			if(q.empty()){//为空无法到达; 
				puts("-1");
				return ;
			}
			fank+=q.top();//中间经过加油站油量和 
			q.pop();
			ans++;
		}
		fank-=d;//剩余油量 
		pos=p[i].a;
		q.push(p[i].b);//将经过点油量计入队列; 
	}
	printf("%d\n",ans);
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d%d",&p[i].a,&p[i].b);
	scanf("%d%d",&x,&y);
	for(int i=0;i<n;i++)//加油站距离车起点时的距离 
	p[i].a=x-p[i].a;
	sort(p,p+n,ac);//从小到大排序 
	solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值