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的更多相关文章
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj 2431 Expedition
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12980 Accepted: 3705 Descr ...
- POJ 2431 Expedition (STL 优先权队列)
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8053 Accepted: 2359 Descri ...
- Expedition(优先队列)
Expedition 点我 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9465 Accepted: 2760 Des ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- CF1091F New Year and the Mallard Expedition
题目地址:CF1091F New Year and the Mallard Expedition 题意比较复杂,整理一下: \(n\) 段,每段有两个属性:长度,地形(G,W,L) 有三种运动方式: ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- Planning The Expedition(暴力枚举+map迭代器)
Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
随机推荐
- jQuery 菜单 垂直菜单实现
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- docker cassandra集群搭建
1.使用daocloud的镜像,创建docker集群 启用一个node docker run -d --name cassandra -p 9042:9042 daocloud.io/library/ ...
- 基于Kubernetes 的Cloud Native 实战 培训课程安排
课程安排: 基于Kubernetes 的Cloud Native 实战 课程介绍: 云计算.虚拟化.容器微服务PaaS 技术已经广泛应用于新兴互联网企业(如电商平台.搜索引擎.社交平台网站.位置服务平 ...
- 数据结构---Java---String
1.概述 1.1 源码(JDK1.8) public final class String implements java.io.Serializable, Comparable<String& ...
- 【多线程】无锁编程以及CAS
无锁编程 / lock-free / 非阻塞同步 无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在没有线程被阻塞的情况下实现变量的同步,所以也叫非阻塞同步(Non-blocking Sy ...
- shell--grep命令+正则表达式+基本语法
什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shell解释的,而正则表达式则 ...
- 洛谷P1122 最大子树和 (树状dp)
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- 通过create-react-app从零搭建react环境
一. 快速开始: 全局安装脚手架: $ npm install -g create-react-app 通过脚手架搭建项目: $ create-react-app <项目名称> 开始项目: ...
- ANSI 标准C 还定义了如下几个宏
ANSI 标准C 还定义了如下几个宏:_LINE_ 表示正在编译的文件的行号_FILE_ 表示正在编译的文件的名字预处理名称意义#define 宏定义#undef 撤销已定义过的宏名#include ...
- Django框架(二十七)—— ContentType组件
目录 ContentType组件 一.什么是ContentType组件 二.使用ContentType 三.使用场景总结 ContentType组件 一.什么是ContentType组件 conten ...