poj,2431Expedition 贪心 + 优先队列
2016-05-12 10:15
267 查看
题意:从起点到终点,途中有n个加油站,每个加油站的加油的油量有限,一个卡车的油箱无限,每走一个单元要消耗一单元的油,问卡车到达终点的最少加多少次油。(注意:加油站的位置是到终点的距离)
解题思路:当油量充足的时候,一直向前走,并且将途中的加油站加的油量放入优先队列(按大到小排序),当没有油了,优先队列弹出顶端的加油,直到加的油保证可以到达下一个加油站或者终点,把终点当做一个加油站,加的油为0。
解题思路:当油量充足的时候,一直向前走,并且将途中的加油站加的油量放入优先队列(按大到小排序),当没有油了,优先队列弹出顶端的加油,直到加的油保证可以到达下一个加油站或者终点,把终点当做一个加油站,加的油为0。
#include <iostream> #include<queue> #include<cstdio> #include<algorithm> using namespace std; #define N 10005 struct u { int a,b; bool operator <(const u& p)const { return a>p.a; } }; u c ; int main() { int n; while(~scanf("%d",&n)) { int l,p; for(int i=1;i<=n;i++) { scanf("%d%d",&c[i].a,&c[i].b); } scanf("%d%d",&l,&p); c[0].a=0; c[0].b=0; sort(c,c+n+1); priority_queue<int>q; int ans=0; int i; for(i=0;i<=n;i++) { if(l-c[i].a<=p) { q.push(c[i].b); } else { if(q.empty()) break; p=q.top()+p; q.pop(); ans++; i--; } } if(p>=l) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- 数据结构_非线性结构_图
- linux设备驱动模块引用和依赖
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- java 构造方法详解 及 new 关键字的租用
- nodejs?
- java.net.SocketException: Connection reset问题解决总结
- MyEclipse修改项目名称后,部署到tomcat问题。
- mybatis反向生成sql,基本的增删改查
- android:descendantFocusability用法简析
- 遇见你,让我的情感得到升温
- loading 进度条
- 口译笔记符号大汇总
- actionbar下面多一条黑线如何去掉
- jquery的冒泡事件event.stopPropagation()
- 47. Permutations II
- 20160512,微软5月10日发布16个安全补丁
- 2016阿里实习生面试经历
- Android学习笔记四:socket通信
- 一个屌丝程序猿的人生(七)
- 【框架】加密解密