ARC-060E - Tak and Hotels - 贪心

题解链接

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 ( 1 i N ) is 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 -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 x i < x 2 < < x N 109
  • x i + 1 x i L
  • 1 a j , b j N
  • a j b j
  • N , L , Q , x i , a j , b j are integers.

Partial Score

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

Input

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

N
x 1 x 2 x N
L
Q
a 1 b 1
a 2 b 2
:
a Q b Q

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 a  j -th hotel to the b j -th hotel.


题意

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


思路

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

扫描二维码关注公众号,回复: 1513766 查看本文章

实现

#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;
}

猜你喜欢

转载自blog.csdn.net/xs18952904/article/details/80385479