#P16060. [CSPro 24] 序列查询新解

[CSPro 24] 序列查询新解

Background

Luogu’s testdata is only for community communication and is not official testdata. Official judging link: https://www.cspro.org/.

Problem Description

In the previous problem “Sequence Query”, it was stated that A=[A0,A1,A2,⋯ ,An]A = [A_0, A_1, A_2, \cdots, A_n] is a sequence of n+1n + 1 integers in the range [0,N)[0, N), satisfying 0=A0<A1<A2<⋯<An<N0 = A_0 < A_1 < A_2 < \cdots < A_n < N. Based on the sequence AA, for any integer xx in the range [0,N)[0, N), the query f(x)f(x) is defined as: the index of the largest number in sequence AA that is less than or equal to xx.

Given a sequence AA and an integer xx, querying f(x)f(x) is a very classic problem, and it can be easily solved with binary search in O(log⁡n)O(\log n) time complexity. However, when the IT department discussed how to implement this function, student Xiao P提出了 some new ideas.

Student Xiao P thinks that if we know in advance how the integers in sequence AA are distributed, we can directly estimate the rough position of the largest integer that is ≤x\le x. Then, starting from this estimated position, we do a linear search to locate f(x)f(x). If the estimate is accurate enough, the time cost of linear search might be smaller than that of binary search.

For example, if A1,A2,⋯ ,AnA_1, A_2, \cdots, A_n are uniformly distributed in the interval (0,N)(0, N), then we can estimate:

$$\begin{aligned} f(x) \approx \frac{(n + 1) \cdot x}{N} \end{aligned}$$

To make computation easier, Xiao P first defines the ratio coefficient r=⌊Nn+1⌋r = \lfloor \frac{N}{n+1} \rfloor, where ⌊⌋\lfloor \rfloor denotes floor, i.e., rr equals the quotient of NN divided by n+1n + 1. Furthermore, Xiao P uses g(x)=⌊xr⌋g(x) = \lfloor \frac{x}{r} \rfloor to represent the estimated value of f(x)f(x). Here, floor is also used to ensure that g(x)g(x) is an integer.

Obviously, for any query x∈[0,N)x \in [0, N), the closer g(x)g(x) and f(x)f(x) are, the more accurate Xiao P’s estimate is, and the smaller the time cost of the subsequent linear search will be. Therefore, Xiao P uses the absolute difference ∣g(x)−f(x)∣|g(x) - f(x)| to represent the error when processing query xx.

To evaluate the overall performance of Xiao P’s method on sequence AA, compute:

$$\begin{aligned} error(A) = \sum_{i=0}^{N-1} |g(i) - f(i)| = |g(0) - f(0)| + \cdots + |g(N - 1) - f(N - 1)| \end{aligned}$$

Input Format

Read data from standard input.

The first line contains two positive integers nn and NN separated by spaces.

The second line contains nn integers A1,A2,⋯ ,AnA_1, A_2, \cdots, A_n separated by spaces.

Note that A0A_0 is fixed as 00, so the input does not include A0A_0.

Output Format

Output to standard output.

Output only one integer, the value of error(A)error(A).

3 10
2 5 8
5
9 10
1 2 3 4 5 6 7 8 9
0
2 10
1 3
6

Hint

Explanation for Sample 1

A=[0,2,5,8]A = [0, 2, 5, 8].

$r = \lfloor \frac{N}{n+1} \rfloor = \lfloor \frac{10}{3+1} \rfloor = 2$.

ii 0 1 2 3 4 5 6 7 8 9
f(i)f(i) 0 1 2 3
g(i)g(i) ^ ^ 2 ^ 3 4
∣g(i)−f(i)∣|g(i) - f(i)| 0 1 0 1

Note: Blank cells in the table mean that this row has no corresponding value in that column (or it is omitted). In actual computation, fill them as needed. According to the statement, g(i)g(i) and ∣g(i)−f(i)∣|g(i)-f(i)| should be defined for all i=0i = 0 to 99. Here we keep the blanks to match the original figure’s structure, but the full computation should be completed:

  • g(0)=⌊0/2⌋=0g(0) = \lfloor 0/2 \rfloor = 0, ∣0−0∣=0|0-0|=0.
  • g(1)=⌊1/2⌋=0g(1) = \lfloor 1/2 \rfloor = 0, ∣0−0∣=0|0-0|=0.
  • g(2)=⌊2/2⌋=1g(2) = \lfloor 2/2 \rfloor = 1, ∣1−1∣=0|1-1|=0.
  • g(3)=⌊3/2⌋=1g(3) = \lfloor 3/2 \rfloor = 1, ∣1−1∣=0|1-1|=0.
  • g(4)=⌊4/2⌋=2g(4) = \lfloor 4/2 \rfloor = 2, ∣2−1∣=1|2-1|=1.
  • g(5)=⌊5/2⌋=2g(5) = \lfloor 5/2 \rfloor = 2, ∣2−2∣=0|2-2|=0.
  • g(6)=⌊6/2⌋=3g(6) = \lfloor 6/2 \rfloor = 3, ∣3−2∣=1|3-2|=1.
  • g(7)=⌊7/2⌋=3g(7) = \lfloor 7/2 \rfloor = 3, ∣3−2∣=1|3-2|=1.
  • g(8)=⌊8/2⌋=4g(8) = \lfloor 8/2 \rfloor = 4, ∣4−3∣=1|4-3|=1.
  • g(9)=⌊9/2⌋=4g(9) = \lfloor 9/2 \rfloor = 4, ∣4−3∣=1|4-3|=1.

So the total sum is: 0+0+0+0+1+0+1+1+1+1=50+0+0+0+1+0+1+1+1+1 = 5.

That is, error(A)=5error(A) = 5.

Explanation for Sample 3

A=[0,1,3]A = [0, 1, 3].

$r = \lfloor \frac{N}{n+1} \rfloor = \lfloor \frac{10}{2+1} \rfloor = 3$.

ii 0 1 2 3 4 5 6 7 8 9
f(i)f(i) 0 1 2 3
g(i)g(i) ^ ^ 2 ^ 3 4
∣g(i)−f(i)∣|g(i) - f(i)| 0 1 0 1

Subtasks

70%70\% of the testdata satisfies 1≤n≤2001 \leq n \leq 200 and n<N≤1000n < N \leq 1000.

All testdata satisfies 1≤n≤1051 \leq n \leq 10^5 and n<N≤109n < N \leq 10^9.

Hint

Note that the input [A1⋯An][A_1 \cdots A_n] is not necessarily uniformly distributed in the interval (0,N)(0, N), so the total error error(A)error(A) may be very large.

Translated by ChatGPT 5