POJ 2431 Expedition
来源:互联网 发布:仿网易云音乐源码 编辑:程序博客网 时间:2024/06/03 18:24
贪心,及优先队列或堆的使用。当路过一个城市或加油站时,不一定需要立刻加油,而可以将其保存在一个优先队列中。当没有油时,再取出优先队列的头加入等量的油,加油的先后是等价的。以加油站为分段点考虑,并将距离转化为距起点的距离。距离需要排序,输入并不保证有序,否则会wrong answer。
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>using namespace std;int n;struct City{int a, b;}city[10010];bool cmp(const City& x, const City& y){return x.a < y.a;}int l, p;void solve() // use the priority queue{int ans = 0, tank = p, pos = 0;priority_queue<int> q;for (int i = 0; i <= n; i++){int d = city[i].a - pos; // distance to next citywhile (tank < d) // need to go to the gas station{if (q.empty()) // no gas station to use{printf("-1\n"); // cannot go to the final cityreturn;}// need to use the gas stationtank += q.top();ans++;q.pop();}// already get to the next citytank -= d;pos = city[i].a;q.push(city[i].b);}printf("%d\n", ans);}int main(){scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d%d", &city[i].a, &city[i].b);scanf("%d%d", &l, &p);for (int i = 0; i < n; i++)city[i].a = l - city[i].a;city[n].a = l;city[n].b = 0;// without sort will cause wrong answer.sort(city, city + n + 1, cmp);solve();}
0 0
- poj 2431 Expedition 贪心
- poj 2431 Expedition
- poj 2431 Expedition
- POJ-2431-Expedition
- poj 2431 Expedition
- POJ 2431 Expedition
- POJ 2431 Expedition(树)
- POJ 2431 Expedition
- poj 2431 Expedition
- POJ 2431 Expedition
- POJ(2431)Expedition
- poj 2431 Expedition
- poj 2431 Expedition
- [POJ 2431]Expedition
- poj 2431 Expedition
- POJ 2431 Expedition
- poj 2431 Expedition
- POJ Expedition 2431
- request.setAttribute()及request.getAttribute()区别
- 01背包问题的动态规划算法
- access函数使用方法
- Eclipse下编写C++程序——CDT环境搭建
- Android Api Demos登顶之路(九十六)Os-->MorseCode
- POJ 2431 Expedition
- c++ include 与 using namespace
- Run grunt from a different directory
- POJ 2752 KMP ? 或 HASH
- 创建mysql表
- Android Api Demos登顶之路(九十七)Text-->Linkify
- Node.js开发入门—Stream用法详解
- android学习路线:如何成长为高级工程师
- thinkphp-生成xml的实例——和js轮换图实现