ATcode E - 高橋君とホテル / Tak and Hotels(倍增预处理,“ X轴上送快递模型 ” )

题目链接:

https://atcoder.jp/contests/arc060/tasks/arc060_c

题目描述:

                                                            E - 高橋君とホテル / Tak and Hotels

Problem Statement

NN hotels are located on a straight line. The coordinate of the

 ii-th hotel (1≤i≤N)(1≤i≤N) is xixi.

Tak the traveler has the following two personal principles:

  • He never travels a distance of more than LL 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 QQ queries. The jj-th (1≤j≤Q)(1≤j≤Q) query is described by two

distinct integers ajaj and bjbj. For each query, find the minimum number of days

that Tak needs to travel from the ajaj-th hotel to the bjbj-th hotel following his

principles. It is guaranteed that he can always travel from the ajaj-th hotel to

the bjbj-th hotel, in any given input.

Constraints

  • 2≤N≤1052≤N≤105
  • 1≤L≤1091≤L≤109
  • 1≤Q≤1051≤Q≤105
  • 1≤xi<x2<...<xN≤1091≤xi<x2<...<xN≤109
  • xi+1−xi≤Lxi+1−xi≤L
  • 1≤aj,bj≤N1≤aj,bj≤N
  • aj≠bjaj≠bj
  • N,L,Q,xi,aj,bjN,L,Q,xi,aj,bj are integers.

Partial Score

  • 200200 points will be awarded for passing the test set satisfying 
  • N≤103N≤103 and Q≤103Q≤103.

Input

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

NN
x1x1 x2x2 ...... xNxN
LL
QQ
a1a1 b1b1
a2a2 b2b2
:
aQaQ bQbQ

Output

Print QQ lines. The jj-th line (1≤j≤Q)(1≤j≤Q) should contain the minimum

number of days that Tak needs to travel from the ajaj-th hotel to the bjbj-th hotel.

Sample Input 1 Copy

Copy

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

Sample Output 1 Copy

Copy

4
2
1
2

For the 11-st query, he can travel from the 11-st hotel to the 88-th hotel in 44 days,

as follows:

  • Day 11: Travel from the 11-st hotel to the 22-nd hotel. The distance traveled is 22.
  • Day 22: Travel from the 22-nd hotel to the 44-th hotel. The distance traveled is 1010.
  • Day 33: Travel from the 44-th hotel to the 77-th hotel. The distance traveled is 66.
  • Day 44: Travel from the 77-th hotel to the 88-th hotel. The distance traveled is 1010.

核心题意:

         在X轴上给出N个旅馆的坐标,一个旅客每天可以走L步,每一天结束时只能停留在有旅馆的位置,

Q次询问,问每次从x-->y,最少要花费几天。

思路:

        最开始,用了最暴力的方法,O(Q*N)TLE,然后二分优化,结果二分最坏情况下

O(Q*N*logN),TLE。

        正解的话是倍增处理状态 dp【i】【j】:从i出发,经过(1<<j)天所能到达的最远位置。

了解过倍增:ST算法和倍增求LCA,但只是当作套路型的东西使用,没有深入了解过)。

        然后就可以O(N*logN预处理),然后O(q*logN)查询。

代码实现:

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int N=2e5+100;
using namespace std;
int a[N];
int dp[N][50];
int main()
{
	int n;
	io;
	while(cin>>n)
	{
		for(int i = 1; i <= n; i++)cin>>a[i];
		int L,Q;
		cin>>L>>Q;
		for(int i = 1; i <= n; i++)
		{
			int id = upper_bound(a+1,a+1+n,a[i]+L)-a-1;
			if(a[i] + L >= a[n])
				dp[i][0] = n;
			else
				dp[i][0] = id;
		}

		for(int i = 1; i <= 30; i++)
		{
			for(int j = 1; j <= n; j++)
				dp[j][i] = dp[dp[j][i-1]][i-1];
		}
		while(Q--)
		{
			int L,R;
			cin>>L>>R;
			if(R<L)
				swap(L,R);
			long long ans=0;
			for(int i = 30; i >= 0; i--)
			{
				if(dp[L][i] < R)
				{
					ans=ans+(1ll<<i);
					L = dp[L][i];
				}
			}
			cout<<ans+1<<endl;
		}
	}
	return 0;
}

The end;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值