高橋君とホテル / 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. myeclipse8.6安装svn插件

    1.从官方网站下载site-1.6.16.zip,网址:subclipse.tigris.org: 2.将解压出来的features与plugins,复制到任意目录:Genuitec/MyEclips ...

  2. PHP之curl函数相关试题

    一.问答题 1.curl_setopt中超时设置,URL设置,post数据接收设置,解压缩设置,HEADER信息设置的参数名分别是什么? 2.curl批量设置参数的函数是什么? 二.编程题 1.封装一 ...

  3. linkButton

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  4. tomcat连接数设置

    如何加大tomcat连接数 在tomcat配置文件server.xml中的<Connector ... />配置中,和连接数相关的参数有:minProcessors:最小空闲连接线程数,用 ...

  5. Unable to chmod /system/build.prop.: Read-only file system

    Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...

  6. python socketserver实现客户端多并发

    直接看代码 server #!/usr/bin/env python # -*- coding:utf-8 -*- import socketserver import subprocess clas ...

  7. ListView控件的Insert、Edit和Delete功能(第一部分)

    摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...

  8. 使用SQL Server Management Studio 创建作业备份数据库

    在项目中,经常需要备份数据库,如果能做到只需点个按钮(“开始备份数据库”按钮),然后什么都不管,数据库就自动备份好了,或者服务器上的数据库隔一段时间自动备份一次,那该多好啊. Sql server 的 ...

  9. c语言的编译过程和GCC 编译参数

    原文: http://www.cnblogs.com/zhangShanGui/p/4912135.html C语言的编译过程和GCC编译参数 C语言的编译一般有三个步骤: 预编译: gcc -E - ...

  10. 转 Linux下的GoldenGate的启动关闭Shell脚本(独立)

    用户想要用OGG进行同步数据,原来用的是Shareplex,至于为啥要换OGG,BulaBula一堆原因.....这不是我们要在意的事情,和客 户装完配置好OGG之后,测试中,客户提出要有个简单的启动 ...