SCU 2401 *** The Story Of Finding Love

想法:用优先队列,对经过的加油站进行排序,然后取最大就ok了

代码如下:

#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<cstring>
#include<sstream>
#include<set>
#include<string>
#include<iterator>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

struct p {
	int d, w;
}s[100010];
int cmp(p a, p b) {
	return a.d < b.d;
}
int main(void) {
	int n, D, W;
	while (cin >> n) {
		cin >> D >> W;
		for (int i = 0; i < n; ++i)
			cin >> s[i].d >> s[i].w;
		s[n].d = D, s[n++].w = 0;//将终点也加进去,避免最后处理终点的问题
		sort(s, s + n, cmp);
		priority_queue<int> p;//优先队列
		int rest = W, ans = 0;//rest为目前水量,ans为最终结果
		for (int i = 0; i < n; ++i) {
			if (rest >= s[i].d)//rest够到下一个加油站,则将下一个加油站的油量加入队列中
				p.push(s[i].w);
			else {
				while (rest < s[i].d) {//不够到下一个加油站,则一直加前面的加油站中的油,直到够
					if (p.empty()) {
						ans = -1; break;//前面所有的加油站的油都加完了还是不够,那么水哥阵亡
					}
					rest += p.top();
					p.pop();
					ans++;
				}
				p.push(s[i].w);
			}
			if (ans == -1)break;
		}
		cout << ans << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值