POJ 2431 Expedition
来源:互联网 发布:淘宝卖家联系电话 编辑:程序博客网 时间:2024/06/05 09:07
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
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
这一题是这样的,就是说一辆车要去town,这时路上有n个加油站,问至少需要在多少个加油站去加油才能到达town,只能直走, 不能回头,没办法到达,输出-1。这边注意一下,题目的输入部分,4,5,11,15是代表到town的距离,所以到这辆车的距离应该是21,20,14,10,开始先按距离排序一下。
这一题可以这么等价,就是把你经过的加油站先记录下来,放在优先队列里,等到油用完时再加上之前已经过的最大的加油站的值,若优先队列为空,则不能到达。类似与贪心的思想差不多。
AC代码:
# include <cstdio># include <queue># include <algorithm>using namespace std;struct node{int d;int f;};node s[10010];int compare(node a, node b){return a.d<b.d;}priority_queue<int, vector<int>, less<int> > q;int main(){int n, i, j, k, l, p, now, ans, flage, cur;while(scanf("%d", &n)!=EOF){for(i=1; i<=n; i++){scanf("%d%d", &s[i].d, &s[i].f);}scanf("%d%d", &l, &p);while(!q.empty()){q.pop();}for(i=1; i<=n; i++){s[i].d=l-s[i].d;}sort(s+1, s+n+1, compare);ans=0;flage=0;now=p;cur=1;while(now<l){for(i=cur; i<=n; i++){if(s[i].d<=now){q.push(s[i].f);}else{cur=i;break;}}if(q.empty()){flage=1;printf("-1\n");break;}else{now=now+q.top();q.pop();ans++;}}if(!flage){printf("%d\n", ans);}}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 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
- Tempter of the Bone
- Logistic Function
- 数据结构之快慢指针查找链表中间结点
- java字符串中如何显示双引号
- 深入源码解析Android中的Handler,Message,MessageQueue,Looper
- POJ 2431 Expedition
- 成功整合dede与ucenter详细步骤
- 南京理工校赛同步 I题 puzzle 2-sat+二分
- nsjson to nsdictionary
- js函数4-闭包
- Android面试题(四)
- 工作中最全最常用的正则表达式
- 消息人士称,Google考虑将Swift作为安卓的主要编程语言
- handler机制详细解释