文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。
贪心算法
Description
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
Output
* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.
Sample Input
4
4 4
5 2
11 5
15 10
25 10
Sample Output
2
Slyar:加油站问题的进阶版,经典加油站问题每个加油站加的油是一样多的,而这里每个加油站加的油是不一样的,很容易考虑到贪心策略是在以前的基础上选择一个加油量最大的加油站加油,因为这样可以开得更远...反正很简单啦。
一开始我直接用N^2的算法写,也过了,后来看到说可以用大堆来维护油量,豁然开朗...最近在学C++,抱着玩的思想,写了一个STL版本,太爽了...我居然还重载了<操作符,哇咔咔...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include <iostream> #include <algorithm> using namespace std; class FuelStop { public: int m_nDis; int m_nFuel; bool friend operator < (const FuelStop& a, const FuelStop& b) { return a.m_nDis > b.m_nDis; } }; int main() { int i, l, p, n, dis, len, sum; int hp[10001]; FuelStop x[10001]; cin >> n; for (int i = 0; i < n; i++) { cin >> x[i].m_nDis >> x[i].m_nFuel; } cin >> l >> p; // 对加油站位置排序 sort(x, x+n); // 剩下的路程 dis = l - p; len = sum = 0; // 忽略加油站在起始点后面的情况 i = 0; while (x[i].m_nDis > l) i++; // 还可以继续前进 while (dis > 0) { // 找到油耗尽前遇到的加油站入堆 for (; i < n; i++) { if (x[i].m_nDis >= dis) { hp[len++] = x[i].m_nFuel; push_heap(hp, hp+len); } // 如果当前加油站不在范围内,则下一个也不在范围内 else break; } // 如果油耗尽前都没有加油站,则失败 if (len == 0) break; // 找加油量最大的加油站加油 dis -= hp[0]; sum++; // 堆顶出堆 pop_heap(hp, hp+len); // 堆大小自减 len--; } if (dis > 0) { cout << "-1" << endl; } else { cout << sum << endl; } return 0; } |
转载请注明:Slyar Home » POJ 2431 Expedition C++版