高橋君とホテル / 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了之后才发现时间复杂度是错的. 但是贪心思想一定是对的,每 ...
随机推荐
- implement a system call in minix
http://www.papervisions.com/implementing-system-call-in-minix-os/
- @font-face的用法
几乎所有浏览器(包括最古老的IE6)也支持的网络字体@font-face的用法是: @font-face { font-family: 'MyWebFont'; src: url('webfont.e ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- Qt Quick 简单教程
上一篇<Qt Quick 之 Hello World 图文详解>我们已经分别在电脑和 Android 手机上运行了第一个 Qt Quick 示例—— HelloQtQuickApp ,这篇 ...
- 在PL/SQL/sqlplus客户端 中如何让程序暂停几秒钟
1. how to check procedure exist: SQL> conn oper/oper123Connected.SQL> desc dbms_lock;PROCEDURE ...
- php非阻塞
PHP非阻塞模式 让PHP不再阻塞当PHP作为后端处理需要完成一些长时间处理,为了快速响应页面请求,不作结果返回判断的情况下,可以有如下措施: 一.若你使用的是FastCGI模式,使用fastcgi_ ...
- Swift 学习笔记(五)
126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...
- equal与==区别
对于String中的“equal方法”和“==”一直有点混肴,今天重新看了一下他们两点的区别,记录下来让自己以后不在忘记! 先说“==”: “==”是用来比较两个String对象在内存中的存放地址是否 ...
- [原]点击按钮,表格隔行变色:偶数行为黄色背景,奇数行为默认颜色。通过table的getElementsByTagName取得所有的tr,依次遍历,如果是偶数就…………。
window.onload = function () { document.getElementById('btn').onclick = function () { ...
- 如何参与Hibernate-ORM项目
1.注册 hibernate jira账户,hibernate中的issue和bug都会在此论坛发布 注册地址:https://hibernate.onjira.com 2.创建Hibernate-O ...