Expedition

给出n+1个整点\(\{x_i\}\)(保证递增排序),一个司机带着初始油量p,从\(x_{n+1}\)出发,每行驶一个单位长度消耗一个油量,其中\(x_1\sim x_n\)为加油站,到达第i个加油站,可以选择获得油量\(a_i\)(不允许重复加油),询问其实你可以这样理解\(a_{n+1}=p\),开始油量为0,询问司机是否能到达原点,如果可以的话,请问最少的加油次数,\(n\leq 10000,1 <= P <= 1,000,000,x_{n+1}\leq 10^6,max(a_i)\leq 100\)。

对于是否可以满足,显然从从右往左扫描,模拟一下即可。

考虑贪心,从右往左扫描,位置当前为x,显然对于现在所有的油量p,可以到达的范围为\([p-x,p]\),接下来我要走的更远,然后就能保证加油次数最少,显然需要取得这个位置范围中油量最大的加油站,设其有油量\(a\),然后可以到达的范围就变为\([p-x-a,p]\),然后\(++ans\),如果原点被这个范围所包括,那么就可以输出答案。

这样就得到一个\(O(n^2)\)做法,显然对于范围中最大的加油站不能暴力扫描,我们可以利用堆动态维护她,然后就可以做到\(nlog(n)\)。

参考代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define il inline
#define ri register
#define Size 15000
using namespace std;
template<class free>
struct heap{
free a[Size];int n;
il void push(free x){
a[++n]=x;ri int p(n);
while(p>1)
if(a[p]>a[p>>1])
swap(a[p>>1],a[p]),
p>>=1;
else break;
}
il void pop(){
a[1]=a[n--];ri int p(1),s(2);
while(s<=n){
if(s<n&&a[s+1]>a[s])++s;
if(a[s]>a[p])
swap(a[s],a[p]),
p=s,s<<=1;
else break;
}
}
};
struct stop{
int x,p;
il bool operator<(const stop&a){
return p>a.p;
}
}s[Size];
heap<int>H;
il void read(int&);
il bool comp(const stop&,const stop&);
int main(){
int n;read(n);
for(int i(1);i<=n;++i)
read(s[i].x),read(s[i].p);
sort(s+1,s+n+1,comp);
int x,p,ans(-1);read(x),read(p),H.push(p);
while(true){
if(!H.n)break;
x-=H.a[1],++ans,H.pop();if(x<=0)break;
while(s[n].x>=x&&n)H.push(s[n].p),--n;
};
if(x>0)puts("-1");
else printf("%d",ans);
return 0;
}
il bool comp(const stop&a,const stop&b){
return a.x<b.x;
}
il void read(int &x){
x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Expedition的更多相关文章

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

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

  2. POJ 2431 Expedition(探险)

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

  3. poj 2431 Expedition

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12980   Accepted: 3705 Descr ...

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

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

  5. Expedition(优先队列)

    Expedition 点我 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 2760 Des ...

  6. poj 3431 Expedition 优先队列

    poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...

  7. CF1091F New Year and the Mallard Expedition

    题目地址:CF1091F New Year and the Mallard Expedition 题意比较复杂,整理一下: \(n\) 段,每段有两个属性:长度,地形(G,W,L) 有三种运动方式: ...

  8. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

  9. Planning The Expedition(暴力枚举+map迭代器)

    Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...

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

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

随机推荐

  1. log4j.xml配置详解(转)

    转自:http://willow-na.iteye.com/blog/347340 Xml代码 <?xml version="1.0" encoding="UTF- ...

  2. Tmux 中开启鼠标选择与复制

    在 tmux.conf 中加入下列设置(tmux 2.1 之前的版本): set -g mode-mouse on set -g mouse-resize-pane on set -g mouse-s ...

  3. MongoDB服务的安装与删除

    服务的安装: 在MongoDB的目录下创建两个文件夹 data和logs, 在通过cmd进入bin目录下,执行命令: mongod --dbpath "C:\Program Files\Mo ...

  4. SVN更新提示内容被锁定

    SVN更新提示内容被锁定   SVN要管理好,并且及时将最新的更新内容上传到SVN上. 在我使用从SVN上更新内容到本地时,总是提示“**********已经锁定”.如果出现这种情况,选择SVN选项“ ...

  5. springboot配置redis+jedis,支持基础redis,并实现jedis GEO地图功能

    Springboot配置redis+jedis,已在项目中测试并成功运行,支持基础redis操作,并通过jedis做了redis GEO地图的java实现,GEO支持存储地理位置信息来实现诸如附近的人 ...

  6. UVa 11806 Cheerleaders (容斥原理+二进制表示状态)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  7. 数组转xls格式的excel文件&数据转csv格式的excle

    /** * 数组转xls格式的excel文件 * @param array $data 需要生成excel文件的数组 * @param string $filename 生成的excel文件名 * 示 ...

  8. mysql授权、删除用户和角色权限

    备份权限 GRANT USAGE ON *.* TO 'backup'@'172.16.0.157' IDENTIFIED BY PASSWORD '*38B4F16EADB1601E713D9F03 ...

  9. 6个实例详解如何把if-else代码重构成高质量代码

    本文提纲: 为什么我们写的代码都是if-else?这样的代码有什么缺点?是否有优化的方法?如何重构?异常逻辑处理型重构方法状态处理型重构方法为什么我们写的代码都是if-else?程序员想必都经历过这样 ...

  10. MySQL允许远程登录配置

    1.查看user表mysql> use mysqlReading table information for completion of table and column namesYou ca ...