日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

高橋君とホテル / Tak and Hotels(AtCoder-2039)

發(fā)布時間:2025/3/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高橋君とホテル / Tak and Hotels(AtCoder-2039) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

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.

Example

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.

題意:n 個酒店在一條直線上,第 i 個酒店坐標是 xi,現在有一個游客,他每天最多能走?L 步,而且必須在一天結束時住進酒店,給出 Q 個詢問,每次給出?a、b 代表第 a 個酒店與第 b 個酒店,問從 a?到 b?最少花費要幾天

思路:

上來就想到了二分,但二分的話時間復雜度仍然會超時

看別人用倍增處理狀態(tài)的做法,dp[i][j] 表示從 i 出發(fā),經過 1<<j 天所能達到的最遠位置,也就是提前處理好 2^x 天數可到達的最遠距離

先預處理 2^0 即第一天可到達的最遠距離,即使用二分來查找可到達的最遠距離

于是,就有狀態(tài)轉移方程:dp[i][j]=dp[dp[i][j-1]][j-1],即第 i 個點的第 2^j 天的最遠距離等于第 i 個點第 2^(j-1) 天到達的點的第 2^(j-1) 天的最遠距離

簡單來說,即:2^j=2^(j-1)*2=2^(j-1)+2^(j-1)

最后求結果時,從后向前找到 a 可到達的最遠小于等于 b?的某點距離,然后不斷的縮小求 x-b 的最短距離統(tǒng)計即可,需要注意的是,由于一開始預處理了第 1 天可到達的最遠距離,因此最終結果要 +1

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 100000+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std;int x[N]; int dp[N][50]; int main(){int n;scanf("%d",&n);for(int i=1; i<=n; i++)scanf("%d",&x[i]);int L,Q;scanf("%d%d",&L,&Q);for(int i=1; i<=n ;i++){//預處理dp[i][0]int left=i+1,right=n;while(left<=right){int mid=(left+right)/2;if(x[mid]<=L+x[i])left=mid+1;elseright=mid-1;}if(x[i]+L>=x[n])dp[i][0]=n;elsedp[i][0]=left-1;}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 a,b;scanf("%d%d",&a,&b);if(b<a)swap(a,b);LL res=0;for(int i=30; i>=0; i--){if(dp[a][i]<b){res+=(1<<i);a=dp[a][i];}}printf("%lld\n",res+1);}return 0; }

?

總結

以上是生活随笔為你收集整理的高橋君とホテル / Tak and Hotels(AtCoder-2039)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。