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.

样例输入

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

样例输出

4
2
1
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

#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 100005;
int ind[maxn],lnh,qry;
const int INF = 1<<30;
int f[maxn][35]; int main() {
// IN_LB();
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",ind+i);
}
scanf("%d%d",&lnh,&qry);
    for(int i=1; i<=n; i++) {
        int idx = (int)(upper_bound(ind+1,ind+n+1, ind[i]+lnh)-ind-1);
        f[i][0] = idx;
    }
for(int j=1; j<=30; j++) {
for(int i=1; i<=n; i++) {
f[i][j] = f[f[i][j-1]][j-1];
}
} for(int q=0; q<qry; q++) {
int a,b;
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
int ans = 0,cur = a;
for(int i=30;i>=0;i--){
if(f[cur][i]<b){
ans+=(1<<(i));
cur = f[cur][i];
}
}
printf("%d\n",ans+1);
}
return 0;
}

【倍增】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. Java基础知识➣泛型整理(四)

    概述 泛型的本质是参数化类型,使用同一套代码来满足不同数据类型的业务需要,提高代码的执行效率,使代码简单明了. 泛型方法 该方法在调用时可以接收不同类型的参数.根据传递给泛型方法的参数类型,编译器适当 ...

  2. 【bzoj4887】[Tjoi2017]可乐 矩阵乘法

    题解: 比较简单的一道题目 如果会倍增floyd这个就很显然的 每次转移看成乘上一个矩阵 另外自爆等同于连到一个特殊点,特殊点只能走自己 停留就是增加自环

  3. noi2018d2t1

    题解: ex-crt 学习见https://www.cnblogs.com/Miracevin/p/9254795.html hdu2891 #include <cstdio> #incl ...

  4. alpha冲刺10/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之10(匆匆而过) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩演练 版本演示视频拍摄 接下来的计划 ...

  5. scrapy 基础使用以及错误方案

    原先用的是selenium(后面有时间再写),这是第一次使用scrapy这个爬虫框架,所以记录一下这个心路历程,制作简单的爬虫其实不难,你需要的一般数据都可以爬取到. 下面是我的目录,除了main.p ...

  6. 035 控制并发 select * from test1 where id =1 for update 就会对这行加锁了?

    今天在看同事程序的时候,看到这种用法,顺便学习下. 一:理论 1.功能 这个功能是上锁. 上的是一个排它锁,也就是说,其他的事务是可以读取的.但是不能写入或者更新. 二:实践 1.创建表 2.提交一条 ...

  7. python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性

    python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性 inspect import inspect def fun(): pass inspect.ism ...

  8. TF之RNN:matplotlib动态演示之基于顺序的RNN回归案例实现高效学习逐步逼近余弦曲线—Jason niu

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIME_STEP ...

  9. Snowflake Snow Snowflakes POJ - 3349(hash)

    You may have heard that no two snowflakes are alike. Your task is to write a program to determine wh ...

  10. Codeforces 1036C Classy Numbers 【DFS】

    <题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R]  (1≤L≤R≤1018).,问你这个区间内有多 ...