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. package.json和package-lock.json的区别

    参考:https://blog.csdn.net/c2311156c/article/details/80320046 package.json: 主要用来定义项目中需要依赖的包 package-lo ...

  2. C语句模拟多任务实例

    #include <stdlib.h> #include <stdio.h> // 任务类型定义 typedef struct _myTask { struct _coeffi ...

  3. Windows下Maven安装 + eclipse集成

    一.什么是maven? Maven是一个项目管理工具,能方便的帮我们下载jar包,告别传统手动导包的方式. 二.maven仓库 maven中有中央仓库,本地仓库,私服三个概念 1.中央仓库是maven ...

  4. get和post 两种基本请求方式的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  5. Database基础(二):MySQL索引创建与删除、 MySQL存储引擎的配置

    一.MySQL索引创建与删除 目标: 本案例要求熟悉MySQL索引的类型及操作方法,主要练习以下任务: 普通索引.唯一索引.主键索引的创建/删除 自增主键索引的创建/删除 建立员工表yg.工资表gz, ...

  6. Django中get()和fiter()的区别

    QuerySet(查询结果集对象):从数据库中查询出来的结果一般是一个集合,这个集合叫做 QuerySet,也就是指服务器上的url里面的查询内容.Django会对查询返回的结果集QuerySet进行 ...

  7. Kali 和 Centos、Windows三系统的安装事项!

    过年了,想在硬盘上直接装Kali Linux,就不用每次插U盘进LiveCD了,但是安装过程真的是!!What fucking word I can say!! 先是分区问题,ntfs有四个分区,其中 ...

  8. context和getApplicationContext()的区别

    在android中常常会遇到与context有关的内容 浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder ...

  9. leetcode上的一些动态规划

    70-爬楼梯 思路:该问题可以理解为经典的“斐波那契数列”问题,但这里需要用动规实现,递归会超时 class Solution { public: int climbStairs(int n) { v ...

  10. linux中软连接和硬链接的区别

    linux中创建软连接和硬链接的方法: 软连接: ln -s oldfile slink 硬连接: ln oldfile hlink linux中创建软连接和硬链接的区别:        原理上,硬链 ...