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

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 犯罪人逃到美国怎么办 10岁儿童偏胖怎么办 土地面积与实际面积不符怎么办 军转进省直单位双选失败怎么办 房改房楼龄到了70年怎么办 商品房房改房到70年后怎么办 公务员未享受房改政策怎么办 两套房改房违规怎么办 法院保全的物品在我家怎么办 西瓜视频答题结束了钱没提现怎么办 诚信答题密码忘了怎么办 我未成年杀了人怎么办 错过了网课考试怎么办? 2018广东省普法考试0分怎么办 小孩错过了小升初的考试怎么办 错过了自主招生考试怎么办? 科目一考试错过了时间怎么办 中学生网瘾怎么办济南远大认真 u盘安装不了系统怎么办 对敏感脆弱的青春期孩子怎么办? 脸变得很交黑怎么办 玩手机长痘痘了怎么办 青少年左侧后背突发疼痛怎么办 纵欲过度导致青少年白发怎么办 20岁了不想长大怎么办 网吧老板跑路了怎么办 网吧玩地下城卡怎么办 个人公积金封存之前厂子欠费怎么办 学生欠了2万块怎么办 玉米去完库存量cool怎么办 幸福蓝海电子券过期怎么办 幸福蓝海国际影城会员怎么办 幸福蓝海会员卡过期了怎么办 电机线圈处于平衡位置怎么办 混联电路求电功率最小怎么办 电脑超出工作频率范围怎么办 手机红外线感应器坏了怎么办 我的世界左右慢怎么办 发电机自动启停装置故障怎么办 如果自动启停没关发动机涉水怎么办 偏激的人不分手怎么办