POJ 2431 Expedition(优先队列)
来源:互联网 发布:编程入门书籍下载 编辑:程序博客网 时间:2024/04/18 14:08
Expedition
题目链接:
http://poj.org/problem?id=2431
解题思路:
我们稍微变换一下思考方式。在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一次在之后的任意时候都可以加Bi单位汽油的权利”,在解决问题上应该也是一样的。而在之后需要加油时,就认为是在之前经过的加油站加的油就可以了。
那么,因为希望到达终点时加油次数尽可能少,所以当燃料为0了时候再进行加油看上去是一个不错的方法。在燃料为0时,应该使用哪个加油站呢?显然,应该选加油站Bi最大的加油站。
为了高效地进行上述操作,我们可以使用从大到小的顺序依次取出数值的优先队列。
- 在经过加油站i时,往优先队列里加Bi。
- 当燃料箱空了时,
(2)否则取出优先队列中最大元素,并用来给卡车加油。
AC代码:
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>using namespace std;int n,l,p;struct node{ int dis,val;}no[10005];bool cmp(node x,node y){ return x.dis < y.dis;}void solve(){ int sum = p,ans = 0; no[n].dis = l;no[n++].val = 0; priority_queue<int> q; for(int i = 0; i < n; i++){ while(sum < no[i].dis){ if(q.empty()){ printf("-1\n"); return; } sum += q.top(); q.pop(); ans++; } q.push(no[i].val); } printf("%d\n",ans);}int main(){ while(~scanf("%d",&n)){ int x,y; for(int i = 0; i < n; i++) scanf("%d%d",&no[i].dis,&no[i].val); scanf("%d%d",&l,&p); for(int i = 0; i < n; i++) no[i].dis = l - no[i].dis; sort(no,no+n,cmp); solve(); } return 0;}
0 0
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- poj 2431 Expedition(优先队列)
- poj 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心、优先队列)
- POJ 2431 Expedition(优先队列)
- POJ - 2431 Expedition(优先队列)
- poj 2431Expedition(优先队列+贪心)
- POJ 2431 Expedition (贪心、优先队列)
- poj-2431Expedition(加油站 优先队列)
- POJ 2431Expedition(优先队列解题)
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition(贪心+优先队列)
- POJ 2431 Expedition (贪心 + 优先队列)
- POJ 2431 Expedition (优先队列 + 贪心)
- POJ 2431 Expedition(贪心+优先队列)
- poj 2431 Expedition(优先队列+贪心)
- poj 2431 Expedition(优先队列)
- Oracle EBS Interface/API(3)--采购订单审批
- UIView中常见的方法总结
- C++笔试总结-面试笔试常考题型(一)指针-引用-宏定义-sizeof
- Tree
- Random类和Math.radnom()方法的联系
- POJ 2431 Expedition(优先队列)
- HDU 3743 Frosh Week 【归并排序模板题】
- 20150812-Mapreduce笔记
- jquery Deferred 数组
- Service的使用(一)
- 在.gitignore中过滤不必要的文件
- 又见回文
- hdu5373
- 在CodeWarrior中指定堆栈大小及堆栈指针