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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6463: Tak and Hotels II(倍增)

發(fā)布時間:2025/3/19 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6463: Tak and Hotels II(倍增) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述
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.
輸入
The input is given from Standard Input in the following format:
N
x1 x2 … xN
L
Q
a1 b1
a2 b2
:
aQ bQ
輸出
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.
樣例輸入

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

樣例輸出

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.

題意:給你直線上一些點(diǎn)的坐標(biāo),一天最大的移動距離,要求每天結(jié)束時必須在一個點(diǎn)上。1e5次詢問,每次詢問從第a個點(diǎn)到第b個點(diǎn)最少要幾天。

解題思路

dp[x][y]表示第x個點(diǎn)2^y次方的天數(shù)能到的最遠(yuǎn)點(diǎn)。暴力或二分預(yù)處理dp[x][0],然后 dp[x][i] = dp[f[x][i-1]][i-1];
查詢的時候, 找到從cur(當(dāng)前點(diǎn))出發(fā)的第一個小于b位置的dp[cur][j],最后答案加上1(2^0)

//dp[x][y]表示第x個點(diǎn)2^ y次方的天數(shù)能到的最遠(yuǎn)點(diǎn)。
dp[j][i]=dp[dp[j][i?1]][i?1];dp[j][i] = dp[dp[j][i - 1]][i - 1];dp[j][i]=dp[dp[j][i?1]][i?1];
// 含義以i等于1為例,意義是第j個點(diǎn)2天能到的最遠(yuǎn)點(diǎn)。那么這不就是我一天最遠(yuǎn)能走到的點(diǎn),的一天能走到的最遠(yuǎn)的點(diǎn)嘛
//其余的就可以依次類推

代碼

int main() {while (cin >>n) {for (int i = 1; i <= n; ++i)cin >> a[i];cin >> L >> Q;for (int i = 1; i <= n; ++i) {//upper_bound()是找到第一個大于的位置,-a得到下標(biāo),再減1得到最后一個小于等于的點(diǎn)int id = upper_bound(a + 1, a + 1 + n, a[i] + L)-a-1;if (a[i] + L >= a[n]) //如果可以一次過那么在一天內(nèi)就可以直接到n點(diǎn)了dp[i][0] = n;elsedp[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--) {cin >> x >> y;if (x > y) swap(x, y);ans = 0;//從大到小枚舉for (int i = 30; i >= 0; --i) {if (dp[x][i] < y) //從第一個點(diǎn)走的2^i的天數(shù)如果小于y,那么這個天數(shù)對于x點(diǎn)最優(yōu){ans += (1) << i;x = dp[x][i]; //如果還沒到終點(diǎn)以這個點(diǎn)來找} }cout << ans+1 << endl;} }return 0; }

lower_bound( )和upper_bound( )都是利用二分查找的方法在一個排好序的數(shù)組中進(jìn)行查找的。

在從小到大的排序數(shù)組中,

lower_bound( begin,end,num):從數(shù)組的begin位置到end-1位置二分查找第一個大于或等于num的數(shù)字,找到返回該數(shù)字的地址,不存在則返回end。通過返回的地址減去起始地址begin,得到找到數(shù)字在數(shù)組中的下標(biāo)。

upper_bound( begin,end,num):從數(shù)組的begin位置到end-1位置二分查找第一個大于num的數(shù)字,找到返回該數(shù)字的地址,不存在則返回end。通過返回的地址減去起始地址begin,得到找到數(shù)字在數(shù)組中的下標(biāo)。

總結(jié)

以上是生活随笔為你收集整理的6463: Tak and Hotels II(倍增)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。