Expedition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18655   Accepted: 5405

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

  1. 4
  2. 4 4
  3. 5 2
  4. 11 5
  5. 15 10
  6. 25 10

Sample Output

  1. 2

Hint

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

 
 
 
 
 
 
题意:我现在开着汽车,车上有p升油,离目的地有l公里,每升油能跑1公里。
    中途会有n个加油站,第i个加油站距目的地a[i]公里,可以加b[i]升油。
    问:能否到达终点,如果能到输出最少加油次数。
 
 
解析:我们可以每次都把当前的油跑完,然后看经过了哪些加油站,找一个能加最多的加油(假设我当时就加过油),
    然后继续跑,记录加油的次数即可。
 
    可以用multiset或者优先队列解这道题,即存走过的加油点。
 
代码:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <map>
  4. #include <vector>
  5. #include <set>
  6. using namespace std;
  7. typedef long long ll;
  8. #define INF 2147483647
  9.  
  10. struct node{
  11.  
  12. int a;int b;
  13. }s[];
  14.  
  15. multiset <int> t;
  16. multiset <int>::iterator it;
  17.  
  18. bool cmp(node x,node y){
  19. return x.a < y.a;
  20. }
  21.  
  22. int main(){
  23. int n,l,p;
  24. cin >> n;
  25. for(int i = ;i < n; i++){
  26. cin >> s[i].a >> s[i].b;
  27. }
  28. cin >> l >> p;
  29. for(int i = ;i < n; i++){
  30. s[i].a = l-s[i].a;
  31. }
  32. sort(s,s+n,cmp);
  33.  
  34. int ans = ;
  35.  
  36. int con = p;
  37. int k = ;
  38. while(con < l){
  39. for(;s[k].a <= con; k++){
  40. int e = s[k].b;
  41. t.insert(e);
  42. }
  43. if(t.size() < ){
  44. cout << - << endl;
  45. return ;
  46. }
  47. it = t.end();it--;
  48. con += *it;
  49. ans ++;
  50. t.erase(it);
  51. }
  52. cout << ans << endl;
  53. return ;
  54. }

POJ 2431 Expedition (priority_queue或者multiset可解)的更多相关文章

  1. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  2. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  3. POJ 2431 Expedition (STL 优先权队列)

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8053   Accepted: 2359 Descri ...

  4. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  5. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  6. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  7. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  8. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

  9. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

随机推荐

  1. Codeforces 232E - Quick Tortoise bitset+分治

    题意: 思路: //By SiriusRen #include <cstdio> #include <bitset> #include <vector> using ...

  2. Safari new Date() 兼容问题

    我的时间 var myTime = "2015-12-31 12:10:21"; 正常写法 var  newTime =  new Date(myTime); safari兼容写法 ...

  3. Sqlite基本命令集合(linux/fedora/ubuntu)

    注:fedora自带sqlite3,无需安装,直接输入命令sqlite3即可. ------------Ubuntu在命令行输入sqlite3,确认没有安装在进行--- 1.安装sqlite3 ubu ...

  4. Pyhton学习——Day9(阶段性练习)

    # 1.文件内容如下,标题为:姓名,性别,年纪,薪资## egon male 18 3000# alex male 38 30000# wupeiqi female 28 20000# yuanhao ...

  5. Vue学习之路第四篇:v-html指令

    上一篇我们讲解了两种方式,把Vue对象的数据展示在页面上: 1.插值表达式 2.v-text指令 但是如果我们展示的数据包含元素标签或者样式,我们想展示标签或样式所定义的属性作用,该怎么进行渲染,比如 ...

  6. php 密码hash加密

    做密码加密,记录一下. password_hash 函数在 PHP 5.5 时被引入. 此函数现在使用的是目前 PHP 所支持的最强大的加密算法 BCrypt .例子: $passwordHash = ...

  7. 代理上网环境配置docker私有库

    最后更新时间:2018年12月27日 Docker使用代理上网去 pull 各类 images,需要做如下配置: 创建目录: /etc/systemd/system/docker.service.d ...

  8. Android开发进度04

    1,今日:目标:实现登录和注册功能 2,昨天:完成登录和注册的界面以及后台数据库的操作 3,收获:会使用SQlite数据库的操作语句 4,问题:登录时出现问题(登录不上去)

  9. ES6之用let,const和用var来声明变量的区别

    var(掌握) 不区分变量和常量   用var声明的变量都是变量,都是可变的,我们可以随便对它进行运算操作.这样当多个人进行同一个项目时,区分变量和常量会越来越难,一不小心就会把设计为常量的数据更改了 ...

  10. (七)u-boot2013.01.01 for s5pv210:《u-boot启动流程》

    转载请注明地址:http://blog.csdn.net/zsy2020314/article/details/9824035 1.关于启动流程 1.1 启动阶段分为3个,bl0,bl1,bl2.下面 ...