poj 2431 Expedition 贪心 + 优先队列

把卡车油用完,然后把 到终点距离大于车到终点距离的全部油站的油 按 油的多少 推进优先队列,然后 取出顶端,车到终点距离减掉这部分油,再进行添加 经过的油站,取最多的油,继续走,,,知道l<=0 或者 队列为空    -



 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

struct node
{
	int fule;
	int dis;
	bool operator < (const node & b) const
	{
		return fule<b.fule;
	}
};
node tm[10000+5];
int cmp(node a,node b)
{
	return a.dis>b.dis;
}
priority_queue <node> qq;
int main()
{

int l,p,n;
	scanf("%d",&n);int i;
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&tm[i].dis,&tm[i].fule);
	}
	scanf("%d%d",&l,&p);
		sort(tm+1,tm+1+n,cmp);

		if (p>=l) {printf("0\n");return 0;}
 	int index=1;
			l-=p;
			int cun=0;
				while(l<=tm[index].dis)
			{
				qq.push(tm[index]);
				index++;
			}
		while (l>0&&!qq.empty())
		{
			
			node tmp=qq.top();
			qq.pop();
			cun++;
			l-=tmp.fule;
			
		
			while(l<=tm[index].dis&&index<=n)
			{
				qq.push(tm[index]);
				index++;
			}

		}
		if (l<=0)	 printf("%d\n",cun); 
else
printf("-1\n");




	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值