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. ...
随机推荐
- Spring MVC 配置Controller详解
在SpringMVC中,对于Controller的配置方式有很多种,如下做简单总结 第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置: <!-- 表示将请求的URL ...
- 三、Centos7安装Mysql
1.到服务器下载的链接 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.执行命令 sudo r ...
- error: device unauthorized.
1 执行 adb install com.taobao.taobao_250.apk 报错 2 先看手机是不是未授权,执行命令之后,手机回弹出授权信息,点击确认就行了
- 微信小程序 textarea的placeholder层级过高 在弹层之上 bug解决方法
微信小程序textarea的placeholder的层级一直都是一个神坑, 我们是没有办法将我们的弹层加大层级去盖过placeholder的, 所以要解决这个问题只能从另外的角度找思路 我的思路是 : ...
- Guarded Suspension模式简单实现
Guarded Suspension 意为保护暂停,假设服务器很短时间内承受大量的客户端请求,客户端请求的数量超过服务器本身的即时处理能力,而服务器又不能丢弃任何一个客户端请求,此时可以让客户端的请求 ...
- pycharm配色
setting-appearanse-theme-IntelliJ setting-Editer-color scheme-Default setting-Editer-color scheme-py ...
- Elasticsearch:使用function_score及soft_score定制搜索结果的分数
我们将介绍使用function_score的基础知识,并介绍一些function core技术非常有用和有效的用例. 介绍 评分的概念是任何搜索引擎(包括Elasticsearch)的核心.评分可以粗 ...
- Halo(三)
接口中可以定义方法 1. 定义静态方法(直接调用) public interface Test { public static void method() { /** * 1.定义一个静态的带有方法体 ...
- redis-持久化、主从复制、集群
持久化 RDB.AOF(redis.conf) 主从复制 redis.conf 集群 redis-trib.rb.ruby相见如下链接http://www.cnblogs.com/wuxl360/p/ ...
- 2018icpc南京/gym101981 A Adrien and Austin 博弈
题意: n个连续排列的石子,每次只许拿连续的(中间没有空格)的k个,问你谁必胜 题解: 简单博弈,特判总数为0,k=1两种情况,其他情况先拿必胜,方法是拿掉中间的,然后对方怎么拿你镜面拿就行. #in ...