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. [转] 梦里Babel知多少(一)

    平时开发中,经常需要用到ES6/ES7的语法.那么就需要用到Babel来对代码进行转码处理. 之前用Vue比较多,所以以Vue-cli作为参考来分析.  第一张图是几个月前的Vue-cli生成的 第二 ...

  2. appstore跳转

    二维码跳转 https://itunes.apple.com/cn/app/id123123123 应用内跳转 this.alertCtrl.create({ title: '更新', message ...

  3. PHP远程下载图片,微信头像存到本地,本地图片转base64

    方法一(推荐): function download_remote_pic($url){ $header = [ 'User-Agent: Mozilla/5.0 (Windows NT 6.1; W ...

  4. BZOJ4816 [Sdoi2017]数字表格 数论 莫比乌斯反演

    原文链接http://www.cnblogs.com/zhouzhendong/p/8666106.html 题目传送门 - BZOJ4816 题意 定义$f(0)=0,f(1)=1,f(i)=f(i ...

  5. Python图表数据可视化Seaborn:3. 线性关系数据| 时间线图表| 热图

    1. 线性关系数据可视化 lmplot( ) import numpy as np import pandas as pd import matplotlib.pyplot as plt import ...

  6. (转)thymeleaf中的判断总结

    判断String字符串,添加引号 th:class="${flag=='forum.html'}?'active'" 判断boolean类型,注意不能当成字符串处理,不能添加引号 ...

  7. poi设置打印页页脚和页数设置

    之前在网上搜了很久,也没有搜到具体页脚页数的答案,最后还是在官方api文档上找到了答案: HSSFPrintSetup printSetup = (HSSFPrintSetup) sheet.getP ...

  8. server client 套接字连接

    server端: 1. 阻塞型套接字,不能满足多个客户端同时访问 import socket server = socket.socket() server.bind((""127 ...

  9. Fruit Ninja(随机数rand())

    链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 题目描述 Fruit Ninja is a juicy action game enjoyed ...

  10. 003.Ansible基础使用

    一 Ansible命令用法 Ansible命令行执行方式有:Ad-Hoc.Ansible-playbook两种,Web方式其官方提供付费产品Tower.Ad-Hoc主要用于临时命令的执行,Ansibl ...