poj2431 Expedition

题目大意:
一个坐标轴 要到0点 车子有初始的位置和油量 有n个加油站 每个最多可以加pi油 求到达0点最少需要加的次数 (油箱unlimit)

分析:
首先加油肯定是能加多少就加多少 对于路过的加油站我们放入待选区 等到我们需要他们的时候就加油 肯定是选之前所有路过能加最多的优先选择

用一个优先队列进行维护就好

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define lowbit(x) x&(-x)
#define ll long long
const int maxn=1e4+5;
struct node{
	int pos,fuel;
}a[maxn];
bool cmp(node a,node b){
	return a.pos>b.pos;
}
priority_queue<int,vector<int>,less<int> >Q;
int main(){
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%d%d",&a[i].pos,&a[i].fuel);
	sort(a+1,a+1+n,cmp);
	a[n+1].pos=0;
	int now,have;
	cin>>now>>have;
	for(int i=1;i<=n+1;i++){
				while(!Q.empty()&&now-have>a[i].pos){
					have+=Q.top();ans++;
					Q.pop();
				}
				if(now-have>a[i].pos){
				cout<<"-1"<<endl;
				return 0;
				}
				now-=have;have=0;
		Q.push(a[i].fuel);
	}
	cout<<ans<<endl;
     return 0;
}

posted @ 2022-05-23 14:58  wzx_believer  阅读(23)  评论(0)    收藏  举报