当前位置: 首页 > 工具软件 > TAK > 使用案例 >

ARC-060E - Tak and Hotels - 贪心

郭兴平
2023-12-01

题解链接

https://www.lucien.ink/archives/229/


题目链接

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


题目

Problem Statement

N hotels are located on a straight line. The coordinate of the i-th hotel (1iN) ( 1 ≤ i ≤ N ) is xi x i .

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 j -th (1jQ) 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

  • 2N105 2 ≤ N ≤ 105
  • 1L109 1 ≤ L ≤ 109
  • 1Q105 1 ≤ Q ≤ 105
  • 1xi<x2<<xN109 1 ≤ x i < x 2 < … < x N ≤ 109
  • xi+1xiL x i + 1 − x i ≤ L
  • 1aj,bjN 1 ≤ a j , b j ≤ N
  • ajbj a j ≠ b j
  • N,L,Q,xi,aj,bj N ,   L ,   Q ,   x i ,   a j ,   b j are integers.

Partial Score

200 200 points will be awarded for passing the test set satisfying N103 N ≤ 10 3 and Q103 Q ≤ 10 3 .

Input

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

N N
x1 x2 x 2 xN
L L
Q
a1 a 1 b1 b 1
a2 a 2 b2 b 2
: :
aQ bQ b Q

Output

Print Q lines. The j-th line (1jQ) ( 1 ≤ j ≤ Q ) should contain the minimum number of days that Tak needs to travel from the aj a j -th hotel to the bj b j -th hotel.


题意

  有n个点,第i个点的坐标为x[i],有一个人每天最多走L米,他每天必须从某个点出发,然后再终止于某个点,有Q个询问,代表他的起点和终点,问最少需要多少天可以到达。


思路

  预处理一下,倍增的过程中统计答案即可,路径不唯一,但答案一定是唯一的。


实现

#include <bits/stdc++.h>
const int maxn = int(1e5) + 7, inf = 0x3f3f3f3f;
int n, x[maxn], up[maxn][20], l, q, u, v, e[27] = {1}, ans, tmp;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", x + i);
    scanf("%d%d", &l, &q);
    memset(up, 0x3f, sizeof(up));
    int cur = 1;
    for (int i = 1; i <= n; i++) while (x[i] > x[cur] + l) up[cur++][0] = i - 1;
    for (int j = 1; j < 20; j++) {
        e[j] = e[j - 1] << 1;
        for (int i = 1; i <= n; i++) if (up[i][j - 1] < inf)
            up[i][j] = up[up[i][j - 1]][j - 1];
    }
    while (ans = 0, q--) {
        scanf("%d%d", &u, &v);
        if (u > v) std::swap(u, v);
        for (int i = 19; i >= 0; i--) if (up[u][i] < v) u = up[u][i], ans += e[i];
        printf("%d\n", ans + 1);
    }
    return 0;
}
 类似资料: