poj2431Expedition
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
中文意思:辆卡车要行驶L单位距离。最开始时,卡车上有P单位汽油,每向前行驶1单位距离消耗1单位汽油。如果在途中车上的汽油耗尽,卡车就无法继续前行,即无法到达终点。途中共有N个加油站,加油站提供的油量有限,卡车的油箱无限大,无论加多少油都没问题。给出每个加油站距离终点的距离和能够提供的油量,问卡车从起点到终点至少要加几次油?如果不能到达终点,输出-1。
解:优先队列+贪心;
每过一个加油站,就拥有了这个加油站的加油权利,当什么时候油不够时,就将经过的加油站中挑出最大的进行加油。
AC代码:
include
include
include
using namespace std;
pair<int ,int > a[10005];
int cmp(pair<int ,int > &a,pair<int ,int > &b){
return a.first<b.first;
}
int main()
{
int n,b;
while(cin>>n){
priority_queue<int,vector,less >q;
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].first,&a[i].second);
int k,f;
cin>>k>>f;
for(int i=1;i<=n;i++)
a[i].first=k-a[i].first;
a[0].first=0;
sort(a,a+n+1,cmp);
int s=f,sum=0,plug=0;
a[n+1].first=k,a[n+1].second=0;
for(int i=1;i<=n+1;i++){
if(s>=a[i].first) q.push(a[i].second);
else{
int plu=1;
while(!q.empty()){
int c=q.top();s+=c;q.pop();sum++;
if(s>=a[i].first){
plu=0;
q.push(a[i].second);
break;
}
}
if(plu) {
cout<<"-1"<<endl;
plug=1;
break;
}
}
}
if(!plug) cout<<sum<<endl;
}
return 0;
}
注意:sort是左闭右开,在这里错了一个小时了。。。。。
poj2431Expedition的更多相关文章
- POJ2431--Expedition(优先队列)
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- 【优先队列+贪心】POJ2431-Expedition
解题方法详见<挑战程序设计竞赛(第二版)>P74-75.注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站. /*优先队列*/ ...
- POJ2431-Expedition【优先队列+贪心】
题目大意:卡车每走一公里就消耗一单位的汽油,初始时给你p单位油,你要到达l距离的终点.其中路上有n个补给点可以加油,并且油箱容量无限大,问你最少可以停车几次. 思路:因为油箱无限大,所以我们可以这么认 ...
随机推荐
- 进程通信(multiprocessing.Queue)
from multiprocessing import Queue Queue([maxsize]) 创建共享的进程队列.maxsize是队列中允许的最大项数.如果省略此参数,则无大小限制.底层队列使 ...
- 12、前端知识点--MVVM模式
1.MVVM与MVC的区别是什么? 在MVC里,View是可以直接访问Model的!从而,View里会包含Model信息,不可避免的还要包括一些业务逻辑. MVC模型关注的是Model的不变,所以,在 ...
- javascript中无法将string转化为json对象
在一次项目之中,我要对请求的相应做一些处理,得到的响应差不多是这中格式'{total:1,result:[{"age":1}]}'.可以看到我拿到的这个相应和JSON的格式是非常相 ...
- Webpack Loader种类以及执行顺序
我们在用webpack构建项目的时候,有两种配置打包文件的方式: import或者require :a-loader!b-loader!.././static/dog.png(打包某一个文件) 配置w ...
- 读书笔记二、pandas之DataFrame
注:DataFrame的构造方法与Series类似,只不过同时接受一条一维数据源,每一条都会成为单独的一列. 注意,返回的Series拥有原DataFrame 相同的索引,且其name属性也已经被相应 ...
- ps:图层知识
如果我们要改变下左图上方的蓝色小球位置,就需要先创建一个符合小球大小的选区,这并不困难,可以使用椭圆选框工具来创建一个正圆的选区(可通过[空格 CTRL 单击图像]放大图像).之后使用移动工具移动选区 ...
- [sqlmap 源码阅读] heuristicCheckSqlInjection 探索式注入
上面是探索式注入时大致调用过程,注入 PAYLOAD 1.)("'(.((. , 数据库报错,通过报错信息获取网站数据库类型(kb.dbms),并将保存报错(lasterrorpage). ...
- Multisim
万用表 测量电压.电流.电阻 直流.交流 函数发生器XFG 正极.负极.公共端 可以产生正弦波.三角波和矩形波,可以设置信号参数:频率.占空比.幅度和偏移量等 示波器XSC 双通道示波器 4个连接点, ...
- python开发之virtualenv与virtualenvwrapper
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...
- Baltic2014 sequence
问题描述 输入格式 输出格式 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 数据范围 所求的Z序列为6,7,8,13,14,15,18. R=13 解析&左偏树 ...