高橋君とホテル / 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. 初识mongo

    进入mongo /usr/local/mongodb/bin/mongo --host 查看所有db show dbs 查看当前进入的db db 查看当前db的所有collection show co ...

  2. .Cannot create an NSPersistentStoreCoordinator with a nil model

    今天用coredata事,忽然遇到这个问题:找了一会终于发现问题所在,与大家分享一下 导致这个问题的原因是因为找不到.xcdatamodeld所致,不同的人可能遇到的问题不同 可能原因1: NSURL ...

  3. Android学习笔记之Spinner

    pinner就相当于Html中的下拉列表框,在下面的例子里我们共同学习一下spinner的使用. 属性: 属性名称 描述 android:prompt 该提示在下拉列表对话框显示时显示.(译者注:对话 ...

  4. onPostCreate——Activity彻底运行起来之后的回调

    记得之前想要在Activity布局完成,彻底跑起来之后,再获取当前Activity的窗口中,某个View的宽高,之前用的办法很土,弄个Handler,发个Message出来,使用sendMessage ...

  5. MySQL SQL优化——分片搜索

    DELIMITER $$ DROP PROCEDURE IF EXISTS `test_release`.`Sp_JP_A_NotifyBegin`$$ CREATE DEFINER=`encysys ...

  6. asp:Property解释与例子

    =======================================================================Property Get 语句在 Class 块中,声明构 ...

  7. 使用runloop阻塞线程的正确写法

    使用runloop阻塞线程的正确写法 runloop可以阻塞线程,等待其他线程执行后再执行. 比如: @implementation ViewController{    BOOL end;}…– ( ...

  8. PullToRefreshListView上拉加载、下拉刷新

    说明:此项目使用studio完成的.需要导入library作为依赖,使用了xuitls获得网络请求.使用Pull解析了XML eclipse中的项目: //注意:此刷新功能是使用的第三方的PullTo ...

  9. 【python之路10】python实例练习

    #!usr/bin/env python # -*- coding:utf-8 -*- # 一.元素分类 # # 有如下值集合 [11,22,33,44,55,66,77,88,99,90...], ...

  10. 极光推送,极光IM使用指南(AndroidStudio)

    到官网创建一个应用,然后下载上面的例子程序,对照集成文档,把libs里的jar和so文件放入到本项目的libs下面,记得把jar要as a library,然后配置清单文件,对照着Demo来,配置好之 ...