现在的位置: 首页 > 综合 > 正文

poj 2431 Expedition 贪心+最大堆

2018年04月04日 ⁄ 综合 ⁄ 共 635字 ⁄ 字号 评论关闭

当油量不够时从走过的油站中选最大加油量的

#include<iostream>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define MAX_N 10005
struct node{
	int dist,fuel;
}t[MAX_N];

bool cmp(const node &a,const node &b)
{
	return a.dist<b.dist;
}
int L,P;
int N;
int main()
{
	priority_queue<int> que;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>t[i].dist>>t[i].fuel;
	cin>>L>>P;
	for(int i=0;i<N;i++)
		t[i].dist = L-t[i].dist;
	sort(t,t+N,cmp); 
	
	t[N].dist=L;//将终点看成加油量为0的加油站 
	t[N].fuel=0;
	N++;
	int cur=P;//当前油量 
	int dis=0;
	int ans=0;
	for(int i=0;i<N;i++)
	{
		int d = t[i].dist-dis;
		while(cur-d<0)
		{
			if(que.empty())
			{
				puts("-1");
				return 0;
			}
			cur += que.top();
			que.pop();
			ans++;
		}
		cur -= d;
		dis=t[i].dist;
		que.push(t[i].fuel);
	}
	cout<<ans<<endl;
}

抱歉!评论已关闭.