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. ...
随机推荐
- Linux里lftp总结
lftp的功能比较强大,相比原来用ftp,方便了很多. 1.登陆: lftp ftp://yourname@site pwd:***** 或 open ftp://yourname@site 基本操作 ...
- 第07章 JdbcTemplate
第07章JdbcTemplate 1. 概述 为了使JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架. 作为Spring JDBC框架的核心,JDB ...
- Flink(一)集群配置
三台主机 centos6 已经完成的工作: 防火墙已关闭 主机名修改完毕,ssh免密登陆配置完成 jdk已安装 zookeeper已经部署并运行 hadoop已经部署并运行 版本:flink-1.8. ...
- URAL 1057 Amount of Degrees (数位dp)
Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- 工程师技术(四):配置SMB文件夹共享、多用户Samba挂载、普通NFS共享的实现、安全NFS共享的实现
一.配置SMB文件夹共享 目标: 本例要求在虚拟机 server0 上发布两个共享文件夹,具体要求如下: 1> 此服务器必须是 STAFF 工作组的一个成员 2> 发布目录 /comm ...
- MVC MVC3中 ViewBag、ViewData和TempData的使用和区别 【转】
在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...
- leetcode上的一些分治算法
53- 思路: 95- 思路
- Mac启动时:boot task failed:fsck-safe处理办法
mac系统启动到一半,然后突然关机,查看启动信息发现:boot task failed:fsck-safe 处理方法,clover启动的时候按空格,然后选择sigle mode. 进入使用命令:fsc ...
- jQuery 删除行(带跨行的表格)
jQuery 删除行(带跨行的表格) 实现效果,点击删除按钮后,在保证原来表格结构的基础上,移除当前行. 代码原理: 1.点击行后判断当前行的第一个<td>,是否包含rowspan属性,如 ...