6463: Tak and Hotels II

时间限制: 1 Sec  内存限制: 128 MB

题目描述

N hotels are located on a straight line. The coordinate of the i-th hotel (1≤i≤N) is xi.
Tak the traveler has the following two personal principles:
He never travels a distance of more than L in a single day.
He never sleeps in the open. That is, he must stay at a hotel at the end of a day.
You are given Q queries. The j-th (1≤j≤Q) query is described by two distinct integers aj and bj. For each query, find the minimum number of days that Tak needs to travel from the aj-th hotel to the bj-th hotel following his principles. It is guaranteed that he can always travel from the aj-th hotel to the bj-th hotel, in any given input.

Constraints
2≤N≤105
1≤L≤109
1≤Q≤105
1≤xi<x2<…<xN≤109
xi+1−xi≤L
1≤aj,bj≤N
aj≠bj
N,L,Q,xi,aj,bj are integers.
Partial Score
200 points will be awarded for passing the test set satisfying N≤103 and Q≤103.

输入

The input is given from Standard Input in the following format:
N
x1 x2 … xN
L
Q
a1 b1
a2 b2
:
aQ bQ

输出

Print Q lines. The j-th line (1≤j≤Q) should contain the minimum number of days that Tak needs to travel from the aj-th hotel to the bj-th hotel.

样例输入

  1. 9
  2. 1 3 6 13 15 18 19 29 31
  3. 10
  4. 4
  5. 1 8
  6. 7 3
  7. 6 7
  8. 8 5

样例输出

  1. 4
  2. 2
  3. 1
  4. 2

提示

For the 1-st query, he can travel from the 1-st hotel to the 8-th hotel in 4 days, as follows:
Day 1: Travel from the 1-st hotel to the 2-nd hotel. The distance traveled is 2.
Day 2: Travel from the 2-nd hotel to the 4-th hotel. The distance traveled is 10.
Day 3: Travel from the 4-th hotel to the 7-th hotel. The distance traveled is 6.
Day 4: Travel from the 7-th hotel to the 8-th hotel. The distance traveled is 10.

题意:给你直线上一些点的坐标,一天最大的移动距离,要求每天结束时必须在一个点上。1e5次询问,每次询问从第a个点到第b个点最少要几天。solutionf[x][y]表示第x个点2^y次方的天数能到的最远点。暴力或二分预处理f[x][0],然后 f[x][i] = f[f[x][i-1]][i-1];查询的时候, 找到从cur(当前点)出发的第一个小于b位置的f[cur][j],最后答案加上1(2^0)code

  1. #define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
  2. #define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
  3. #define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 100005;
  8. int ind[maxn],lnh,qry;
  9. const int INF = 1<<30;
  10. int f[maxn][35];
  11. int main() {
  12. // IN_LB();
  13. int n;
  14. scanf("%d",&n);
  15. for(int i=1; i<=n; i++) {
  16. scanf("%d",ind+i);
  17. }
  18. scanf("%d%d",&lnh,&qry);
  19.     for(int i=1; i<=n; i++) {
  20.         int idx = (int)(upper_bound(ind+1,ind+n+1, ind[i]+lnh)-ind-1);
  21.         f[i][0] = idx;
  22.     }
  23. for(int j=1; j<=30; j++) {
  24. for(int i=1; i<=n; i++) {
  25. f[i][j] = f[f[i][j-1]][j-1];
  26. }
  27. }
  28. for(int q=0; q<qry; q++) {
  29. int a,b;
  30. scanf("%d%d",&a,&b);
  31. if(a>b)
  32. swap(a,b);
  33. int ans = 0,cur = a;
  34. for(int i=30;i>=0;i--){
  35. if(f[cur][i]<b){
  36. ans+=(1<<(i));
  37. cur = f[cur][i];
  38. }
  39. }
  40. printf("%d\n",ans+1);
  41. }
  42. return 0;
  43. }

【倍增】Tak and Hotels II @ABC044&ARC060/upcexam6463的更多相关文章

  1. 高橋君とホテル / Tak and Hotels

    高橋君とホテル / Tak and Hotels Time limit : 3sec / Stack limit : 256MB / Memory limit : 256MB Score : 700  ...

  2. AtCoder Beginner Contest 044 A - 高橋君とホテルイージー / Tak and Hotels (ABC Edit)

    Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There is a hotel with ...

  3. 2018.09.17 atcoder Tak and Hotels(贪心+分块)

    传送门 一道有意思的题. 一开始想错了,以为一直lowerlowerlower_boundboundbound就可以解决询问,结果交上去TLE了之后才发现时间复杂度是错的. 但是贪心思想一定是对的,每 ...

  4. AtCoder Tak and Hotels

    题目链接:传送门 题目大意:有 n 个点排成一条直线,每次行动可以移动不超过 L 的距离,每次行动完成必须停在点上, 数据保证有解,有 m 组询问,问从 x 到 y 最少需要几次行动? 题目思路:倍增 ...

  5. 【AtCoder】ARC060

    ARC060 C - 高橋君とカード / Tak and Cards 每个数减去A,然后转移N次,每次选或不选,最后是和为0的时候的方案数,负数可以通过把所有数右移2500做到 #include &l ...

  6. AtCoder Regular Contest

    一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...

  7. AtCoder 杂题训练

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

  8. AtCoder-arc060 (题解)

    A - 高橋君とカード / Tak and Cards (DP) 题目链接 题目大意: 有 \(n\) 个数字,要求取出一些数字,使得它们的平均数恰好为 \(x\) ,问有几种取法. 大致思路: 只要 ...

  9. AtCoder Regular Contest 060

    C - 高橋君とカード / Tak and Cards 思路:dp,先说说我想的,我写的dp数组是dp[i][j][k],表示从前i个数字中,选择j个数字,平均值为k,则dp[i][j][k] = d ...

随机推荐

  1. 用jQuery实现Ajax

    前置知识:ajax原理,json字符串进行信息传递. Ajax主要的功能是实现了浏览器端 异步 访问服务器:通过浏览器的XMLHttpRequest对象发出小部分数据,与服务端进行交互, 服务端返回小 ...

  2. .NET轻量级任务管理类

    概述 最近做项目总是遇到服务跑批等需求,一直想写个任务管理的DLL,现在整理了一下思路,编写了一个DLL类库,使用方便.只要调用的子类继承服务基类便可以实现任务的整体调度.先看看页面效果: 使用方式 ...

  3. Python_python内置加密模块

    数据加密: 对称加密:数据加密和解密使用相同的密钥,主要解决数据的机密性(DES,AES) 非对称加密(公匙加密):数据加密和解密使用的不同密钥,主要用于身份的验证(DSA,RSA) 单向加密:只能加 ...

  4. phpMyAdmin 安装教程全攻略

    管理MYSQL数据库的最好工具是PHPmyAdmin,现在最新版本是phpMyAdmin 2.9.0.2,这是一个国际上开源的软件,一直在更新版本,你可以从 http://www.phpmyadmin ...

  5. AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...

  6. 019 python面相对象编程

    一:self的意思 1.说明 self代表类的实例,而非类. 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self. self 代表的是类的实例 ...

  7. pageHelper多个sql分页

    之前有个需求,在一个页面中需要有多个sql分页查询然后放到一个list中,展示,但是会出现一个bug,就是每次分页都会展示第一条查出的所有的数据: 第一页 第二页 因为是截的生产环境,第一条数据被处理 ...

  8. 12306登录爬虫 session版本

    import requests import re import base64 # 定义session headers = { 'User-Agent':'Mozilla/5.0 (Windows N ...

  9. 12306登录爬虫 cookies版本

    import requests import re import base64 cookies = None # 进入主页,保留cookies login_url = 'https://kyfw.12 ...

  10. hibernate.properties not found

    在配置hibernate的主键生成策略的时候突然报出如下错误,寻找了很长时间,虽然不是什么严重的错误,但是希望可以警醒自己 问题: 11:26:21,611 INFO Version:37 - HHH ...