poj 2431 Expedition

解法在注释里

/*
这道题表面上看起来很复杂,对于每个加油站都要选择是否要加油
但可以注意到,每到一个加油站,只要汽车还有油,对于加油和不加油的两种选择,接下来的解法都是一样的
这也就导致了这道题可以用贪心来做
做法:到加油站的时候都暂时不加油,等汽车没油了,再从已经经过的加油站里选一个油量最大的,因为数据比较大,直接比较会超时,所以用优先队列
*/
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX_N 1000000
struct Stop {
	int dist;
	int fuel;
};
int n,l,p;
Stop stop[MAX_N];
priority_queue<int> que;

int cmp(Stop a,Stop b) {
	return a.dist<b.dist;
}
int solve() {
	int ans=0;
	int i=0,post=0;
	while(1) {
		while(post+p>=stop[i].dist && post!=l) {
			p-=stop[i].dist-post;
			post=stop[i].dist;

			que.push(stop[i].fuel);
			i++;
		}
		if (post>=l) break;
		if (que.empty()) return -1;
		p+=que.top();
		que.pop();
		ans++;
	}
	return ans;
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	while(scanf("%d",&n)!=EOF) {
		int i;
		for(i=0;i<n;i++) {
			scanf("%d%d",&stop[i].dist,&stop[i].fuel);
		}
		scanf("%d%d",&l,&p);
		stop[n].dist=l;
		for(i=0;i<n;i++) {
			stop[i].dist=l-stop[i].dist;
		}
		sort(&stop[0],&stop[n],cmp);
		printf("%d\n",solve());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值