高橋君とホテル / Tak and Hotels
高橋君とホテル / 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≤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.
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≤j≤Q) 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的更多相关文章
- 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 ...
- 高橋君とカード / Tak and Cards
高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 p ...
- 高橋君とカード / 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 ...
- AtCoder Beginner Contest 044 C - 高橋君とカード / Tak and Cards
题目链接:http://abc044.contest.atcoder.jp/tasks/arc060_a Time limit : 2sec / Memory limit : 256MB Score ...
- AT987 高橋君
AT987 高橋君 给出 \(n,\ k\) ,求 \(\displaystyle\sum_{i=0}^kC_n^k\) , \(T\) 次询问 \(T\leq10^5,\ 0\leq k\leq n ...
- 【AT987】高橋君
题目 成爷爷一眼秒,\(tql!!!\) 多组询问,求 \[\sum_{i=0}^kC_{n}^i \] 发现\(k<=n\)啊,于是我们可以把一组询问抽象成一个区间\([k,n]\) 左指针的 ...
- AtCoder D - 高橋君と見えざる手 / An Invisible Hand 简单思维题
http://arc063.contest.atcoder.jp/tasks/arc063_b 因为每次都是选取最大值,那么用dp[i]表示第i个数结尾能得到最大是多少. 其实就是用a[i]去减去左边 ...
- 【倍增】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 ...
- 2018.09.17 atcoder Tak and Hotels(贪心+分块)
传送门 一道有意思的题. 一开始想错了,以为一直lowerlowerlower_boundboundbound就可以解决询问,结果交上去TLE了之后才发现时间复杂度是错的. 但是贪心思想一定是对的,每 ...
随机推荐
- screen 链接远程桌面
screen 开一个新的screen窗口 screen -ls 查看已经存在的所有screen窗口 screen -r 208111 进入这个窗口 ctrl+a+d 退出screen,回 ...
- gre tunnel
http://searchenterprisewan.techtarget.com/tip/GRE-tunnel-vs-IPsec-tunnel-What-is-the-difference Enca ...
- STM32F407IG开启FPU,做开方运算
STM32F407IG开启FPU,做开方运算 MDK KEIL中使用STM32F4XX芯片硬件浮点单元FPU Keil中使用STM32F4xx硬件浮点单元 STM32F4-浮点DSP库的MDK开发环境 ...
- Shell学习笔记 ——第一天
1.程序第一行 指定执行Shell的程序 #!/bin/sh #!用来告诉系统它后面的参数是用来执行该文件的程序 2.在控制台输出信息 echo "Hello Shell" #! ...
- zf-分页后台代码
java : public ResultPage getDeptList(int page, int pageRows) throws Exception { String hql="fro ...
- C#后台绑定ComboBox
C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...
- Redis 笔记
Redis是一个key-value存储系统.和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表).sets( ...
- Dockerfile编写语法
docker镜像本质上就是一个个基础镜像的堆叠,为了做出我们想要的镜像,我们需要考虑最终镜像所需的所有基础环境,然后一层层堆叠.也就是不断以基础镜像搭建上层镜像. 先看例子: # Version: # ...
- “strcmp()” Anyone?
“strcmp()” Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two ...
- linux shell 远程执行命令
经常要部署多台服务器上面的应用,如果一个个机器的登录太麻烦. 所有就想到编写一个脚本来部署不同的服务器 前提条件: 配置ssh免登陆 如果不会的请参加我的另外一篇文章 http://blog.csdn ...