高橋君とホテル / Tak and Hotels


Time limit : 3sec / Stack limit : 256MB / Memory limit : 256MB

Score : 700 points

Problem Statement

N hotels are located on a straight line. The coordinate of the i-th hotel (1≤iN) 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≤jQ) 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−xiL
  • 1≤aj,bjN
  • ajbj
  • 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.

Input

The input is given from Standard Input in the following format:

N
x1 x2 xN
L
Q
a1 b1
a2 b2
:
aQ bQ

Output

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


Sample Input 1

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

Sample Output 1

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.

分析:对于每个点二分可以找到一天所能到达的最右和最左端点;

   然后关键就是倍增,这样每个小问题就可以在log复杂度解决了;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000000
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,l[][maxn],r[][maxn];
ll a[maxn],p;
void init()
{
memset(l,-,sizeof l);
memset(r,-,sizeof r);
for(int i=;i<=n-;i++)
{
r[][i]=lower_bound(a,a+n,a[i]+p)-a;
if(r[][i]==n||a[r[][i]]>a[i]+p)r[][i]--;
l[][i]=lower_bound(a,a+n,a[i]-p)-a;
if(i==n-)r[][i]=-;
if(i==)l[][i]=-;
}
for(int i=;i<=;i++)
{
for(int j=;j<=n-;j++)
{
if(r[i-][j]<n-)r[i][j]=r[i-][r[i-][j]];
if(l[i-][j]>)l[i][j]=l[i-][l[i-][j]];
}
}
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n-)scanf("%lld",&a[i]);
scanf("%lld",&p);
init();
int q;
scanf("%d",&q);
while(q--)
{
int b,c,ans=;
scanf("%d%d",&b,&c);
b--,c--;
if(b<c)
{
for(i=;i>=;i--)
{
if(r[i][b]!=-&&r[i][b]<=c)
{
ans+=qpow(,i);
b=r[i][b];
if(b==c)break;
}
}
if(i==-)ans++;
}
else
{
for(i=;i>=;i--)
{
if(l[i][b]!=-&&l[i][b]>=c)
{
ans+=qpow(,i);
b=l[i][b];
if(b==c)break;
}
}
if(i==-)ans++;
}
printf("%d\n",ans);
}
//system("Pause");
return ;
}

高橋君とホテル / Tak and Hotels的更多相关文章

  1. 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 ...

  2. 高橋君とカード / Tak and Cards

    高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 p ...

  3. 高橋君とカード / Tak and Cards AtCoder - 2037 (DP)

    Problem Statement Tak has N cards. On the i-th (1≤i≤N) card is written an integer xi. He is selectin ...

  4. AtCoder Beginner Contest 044 C - 高橋君とカード / Tak and Cards

    题目链接:http://abc044.contest.atcoder.jp/tasks/arc060_a Time limit : 2sec / Memory limit : 256MB Score ...

  5. AT987 高橋君

    AT987 高橋君 给出 \(n,\ k\) ,求 \(\displaystyle\sum_{i=0}^kC_n^k\) , \(T\) 次询问 \(T\leq10^5,\ 0\leq k\leq n ...

  6. 【AT987】高橋君

    题目 成爷爷一眼秒,\(tql!!!\) 多组询问,求 \[\sum_{i=0}^kC_{n}^i \] 发现\(k<=n\)啊,于是我们可以把一组询问抽象成一个区间\([k,n]\) 左指针的 ...

  7. AtCoder D - 高橋君と見えざる手 / An Invisible Hand 简单思维题

    http://arc063.contest.atcoder.jp/tasks/arc063_b 因为每次都是选取最大值,那么用dp[i]表示第i个数结尾能得到最大是多少. 其实就是用a[i]去减去左边 ...

  8. 【倍增】Tak and Hotels II @ABC044&ARC060/upcexam6463

    6463: Tak and Hotels II 时间限制: 1 Sec  内存限制: 128 MB 题目描述 N hotels are located on a straight line. The ...

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

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

随机推荐

  1. Ubuntu 网管服务器配置

    1.设置Linux内核支持ip数据包的转发 echo "1" > /proc/sys/net/ipv4/ip_forward or vi /etc/sysctl.conf   ...

  2. div style设置隐藏多余字, title设置鼠标放上显示出来全部

    <div style='overflow: hidden; white-space: nowrap; text-overflow: ellipsis;' title='"+data[j ...

  3. PMBok项目管理

    这就是项目管理的九大领域:整合管理.范围管理.时间管理.费用管理.质量管理.人力资源管理.沟通管理.风险管理.采购管理. 项目管理好像一头大象,将其大卸九块之后,要装进冰箱就容易多了. 看看书上是怎样 ...

  4. maven之pom

    记录一下最近的pom的相关设置,plugin的官方地址配置:http://maven.apache.org/plugins/index.html 看了网上说了很多例子,有很多不清楚,看一下官方的,会有 ...

  5. 设计模式二 适配器模式 adapter

    适配器模式的目的:如果用户需要使用某个类的服务,而这项服务是这个类用一个不同的接口提供的,那么,可以使用适配器模式为客户提供一个期望的接口.

  6. PHP基本类型操作

    //关键字对大小写不敏感echo ('hello world!<br>');ECho ('hello world<br>');eCho ('hello world<br& ...

  7. javascript IP验证

    //IP验证function isIP(strIP){try{if(strIP.length<7){return false;}var re=/^(\d+)\.(\d+)\.(\d+)\.(\d ...

  8. 编译使用tinyxml

    环境: win7 32位旗舰版,VS2010,tinyxml_2_6_2版本 1.下载tinyxml,并解压到tinyxml文件夹下 2.生成动态链接库 原生的Tinyxml只支持静态库(没有在.h文 ...

  9. Swift -> Let & Var 背后编程模式 探讨

    简介 Swift中有两种声明“变量”的方式,这两种方式分别使用let和var这两个关键字.这应该是借鉴了Scala,因为它们和Scala的val和var有相同的作用.let被用于声明不变量,var被用 ...

  10. Java中常见的5种WEB服务器介绍

    这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...