【优先队列】Expedition

Expedition

总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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
输出
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.

样例输入
4
4 4
5 2
11 5
15 10
25 10
样例输出
2
提示
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.
来源
USACO 2005 U S Open Gold

译文如下

描述
一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队。作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱。卡车现在每运行一个单位的距离泄漏一个燃料单位。

为了修理卡车,奶牛需要沿着一条蜿蜒的长路行驶到最近的城镇(距离不超过1,000,000个单位)。在这条路上,在城镇和卡车的当前位置之间,有N1 <= N <= 10,000)燃料站,奶牛可以停下来获得额外的燃料(每站1...100个单位)。

丛林对人类来说是一个危险的地方,对奶牛尤其危险。因此,奶牛希望在前往城镇的路上尽可能少地停下燃料。幸运的是,他们的卡车上的油箱容量非常大,实际上它可以容纳的燃油量没有限制。卡车目前距离城镇L单位,有P单位的燃料(1 <= P <= 1,000,000)。

确定到达城镇所需的最小停留次数,或者奶牛根本无法到达城镇。

输入

1行:单个整数,N

2..N + 1行:每行包含两个以空格分隔的整数,用于描述燃料停止:第一个整数是从城镇到停靠点的距离; 第二个是该站点可用的燃料量。

N + 2行:两个以空格分隔的整数,LP.

输出

一个整数,给出到达城镇所需的最少燃料停留次数。如果无法到达城镇,则输出-1

样例输入

4
4 4
5 2
11 5
15 10
25 10

样例输出

2

提示

输入细节:

卡车距离城镇25个单位; 这辆卡车有10个燃料单位。沿着这条路,距离城镇4,5,11和15的距离有4个燃料站(所以这些距离最初距离卡车的距离为21,20,14和10)。这些燃料停止装置可分别提供多达4个,2个,5个和10个单位的燃料。

输出细节:

驱动10个单位,停止再购买10个单位的燃料,再开4个单位,停止再购买5个单位的燃料,然后开车到镇上。

思路:

只要我经过了这个加油站,可以视作我随时能加这个加油站的油。
那么我可以一直走,没油的时候把前面经过的加油站中油最多的拿出来加掉。
如果我没油可加,那么无法到达城镇。
把经过的加油站放进优先队列维护即可。

AC 代码

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define abss(x) ((x)>(0)?(x):(-1)*(x))
#define maxs(a,b) ((a)>(b)?(a):(b))
#define mins(a,b) ((a)<(b)?(a):(b))
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define mem(a) memset(a,0,sizeof(a))
const int INF (1<<30);
const int inf (-1<<30);
using namespace std;

struct node{
	int di,fu;
}sta[10007];

bool cmp(node x,node y){
	return x.di<y.di;
}

int N,L,P;
priority_queue<int,vector<int>,less<int> > heap;

int main(){
	cin>>N;
	int di,fu;
	FOR(i,0,N-1){
		cin>>di>>fu;
		sta[i]={di,fu};
	}
	cin>>L>>P;
	FOR(i,0,N-1){
		sta[i].di=L-sta[i].di;
	}

	sort(sta,sta+N,cmp);
	sta[N++]={L,0};

	int ans=0,pos=0,fue=P;

	FOR(i,0,N-1){
		int di=sta[i].di-pos;
		while(fue<di){
			if(heap.empty()){
				cout<<-1;
				return 0;
			}
			fue+=heap.top();
			heap.pop();
			ans++;
		}
		fue-=di;
		pos=sta[i].di;
		heap.push(sta[i].fu);
	}
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值