题目链接

题意:
给你 n n n个坐标,要求每次移动不超过 l l l,并且每次移动后一定要到这 n n n个坐标中的某一个。有 q q q次询问,每次询问一对点 a , b a,b a,b,求从 a a a到 b b b最少移动几次。 n , q &lt; = 1 e 5 n,q&lt;=1e5 n,q<=1e5

题解:
首先我们可以用一个two pointer在O(n)的时间内算出以每个点为起点走不超过 l l l的距离最多走到哪个点。感觉网上好多都说要二分,但是我感觉二分还没有two pointer好写,而且跑得还慢。求完只后我们可以对刚才的数组倍增,求出每个点操作 2 i 2^i 2i步最多到达的编号是多少。这样对于每次询问,从 a a a到 b b b和从 b b b到 a a a是等价的,所以我们设 b &gt; = a b&gt;=a b>=a。我们从 a a a先跳到小于 b b b的可以跳到的最后一个点,然后再跳一次就能跳到 b b b了,于是就可以用倍增每次 l o g n logn logn的复杂度来回答询问了。最终复杂度 O ( n + q l o g n ) O(n+qlogn) O(n+qlogn)。

代码:

#include <bits/stdc++.h>
using namespace std;int n,q,a[100010],f[100010][21],l,r;
int main()
{scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d",&a[i]);scanf("%d",&l);r=1;for(int i=1;i<=n;++i){while(a[r+1]-a[i]<=l&&r+1<=n)++r;f[i][0]=r;}for(int i=n;i>=1;--i){for(int j=1;j<=20;++j)f[i][j]=f[f[i][j-1]][j-1];}scanf("%d",&q);for(int i=1;i<=q;++i){int x,y,ans=0;scanf("%d%d",&x,&y);if(x>y)swap(x,y);for(int j=20;j>=0;--j){if(f[x][j]<y){x=f[x][j];ans+=(1<<j);}   }ans+=1;printf("%d\n",ans);}return 0;
}

ARC060 E Tak and Hotels 倍增相关推荐

  1. arc060 E - Tak and Hotels

    E - Tak and Hotels 先找到每个点一步最远跳到哪个点,然后用个倍增即可 #include<bits/stdc++.h> #define pii pair<int,in ...

  2. Tak and Hotels II【倍增】

    Tak and Hotels II 时间限制: 1 Sec 内存限制: 128 MB 提交: 140 解决: 38 [提交] [状态] [命题人:admin] 题目描述 N hotels are lo ...

  3. [AtCoder Regular Contest 060] E - Tak and Hotels

    AT2039 [ARC060C] 高橋君とホテル / Tak and Hotels problem solution - 分块 code - 分块 solution - 倍增 code - 倍增 pr ...

  4. 6463: Tak and Hotels II

    6463: Tak and Hotels II 时间限制: 1 Sec   内存限制: 128 MB 提交: 95   解决: 29 [ 提交][ 状态][ 讨论版][命题人: admin] 题目描述 ...

  5. 6463: Tak and Hotels II(倍增)

    题目描述 N hotels are located on a straight line. The coordinate of the i-th hotel (1≤i≤N) is xi. Tak th ...

  6. 高橋君とホテル / Tak and Hotels(AtCoder-2039)

    Problem Description N hotels are located on a straight line. The coordinate of the i-th hotel (1≤i≤N ...

  7. ARC060 C - Tak and Cards(dp,背包)

    题意: 解法: 令d[i][j][k]表示前i个物品,选j个,总和为k的方案数, 背包dp出来,最后答案为sum(d[n][i][i*A]).第一维可以滚动优化掉. code: #include &l ...

  8. AtCoder Regular Contest 060

    文章目录 C - Tak and Cards D - Digit Sum E - Tak and Hotels F - Best Representation 比赛链接 C - Tak and Car ...

  9. AtCoder 杂题训练

    前言: 因为要普及了,今年没一等就可以退役去学文化课了,所以暑假把历年noip普及组都刷了一遍,离noip还有50+天,想弄点强化训练什么的. 想了想,就这些天学文化课之余有空就把AtCoder之前那 ...

最新文章

  1. 统计模型参数量与FLOPs
  2. [转] ios学习--openURL的使用方法
  3. Boost::context模块callcc的throw测试程序
  4. 对于一颗具有n个结点,度为4的树来说,( )
  5. 一段话系列-领域模型是什么?
  6. GitHub for windows使用教程(三) 团队协作流程
  7. hibernate hql limit的实现方式
  8. mysql mybatis 工具类_我肝了一个星期,为你们整理出了mybatis程序配置教程
  9. react中@withrouter_为什么 withRouter 高阶组件应该 处于最外层?
  10. js html 拼接,JavaScript concat() 方法
  11. 查看端口占用,并解除占用
  12. vmware workstation 12安装ubuntu kylin 16.04虚拟机
  13. 基于HTML仿华为手机网站电商项目的设计与实现
  14. JDK和JRE安装与下载
  15. 联想入选恒生指数成分股
  16. python求圆柱体的体积_面向对象的Python程序计算球体的体积和表面积
  17. ROS——发布摄像头节点并编写opencv图像处理节点(python)
  18. 刷脸支付就是会员为大商户管理与运营提供帮助
  19. MySQL数据库中插入图片
  20. 计算机文化基础008,2013 中国成人教育 翻转课堂在高校计算机文化基础课中的应用研究_刘桂花...

热门文章

  1. Python分支结构
  2. SpringBoot用实体接收Get请求参数
  3. 基于C51单片机开发的智能垃圾桶项目
  4. Mysql中实现全外连接
  5. “看片”神器没了,又将有谁突出重围?
  6. 顶级数学家有多厉害?坐牢、被开除、遭到围攻……但诺贝尔奖依然颁给了他...
  7. 勒索软件安全意识小测验
  8. 卷积层与全连接层的区别
  9. 仅仅有人物没背景的图片怎么弄_设置微信聊天背景里怎么就一张白色的,没有可供下载的背景图...
  10. Could not find feature force_asserts