#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 is a sequence of integers in the range , satisfying . Based on the sequence , for any integer in the range , the query is defined as: the index of the largest number in sequence that is less than or equal to .
Given a sequence and an integer , querying is a very classic problem, and it can be easily solved with binary search in 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 are distributed, we can directly estimate the rough position of the largest integer that is . Then, starting from this estimated position, we do a linear search to locate . If the estimate is accurate enough, the time cost of linear search might be smaller than that of binary search.
For example, if are uniformly distributed in the interval , 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 , where denotes floor, i.e., equals the quotient of divided by . Furthermore, Xiao P uses to represent the estimated value of . Here, floor is also used to ensure that is an integer.
Obviously, for any query , the closer and 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 to represent the error when processing query .
To evaluate the overall performance of Xiao P’s method on sequence , 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 and separated by spaces.
The second line contains integers separated by spaces.
Note that is fixed as , so the input does not include .
Output Format
Output to standard output.
Output only one integer, the value of .
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
.
$r = \lfloor \frac{N}{n+1} \rfloor = \lfloor \frac{10}{3+1} \rfloor = 2$.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | |||||||
| ^ | ^ | 2 | ^ | 3 | 4 | |||||
| 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, and should be defined for all to . Here we keep the blanks to match the original figure’s structure, but the full computation should be completed:
- , .
- , .
- , .
- , .
- , .
- , .
- , .
- , .
- , .
- , .
So the total sum is: .
That is, .
Explanation for Sample 3
.
$r = \lfloor \frac{N}{n+1} \rfloor = \lfloor \frac{10}{2+1} \rfloor = 3$.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | |||||||
| ^ | ^ | 2 | ^ | 3 | 4 | |||||
| 0 | 1 | 0 | 1 | |||||||
Subtasks
of the testdata satisfies and .
All testdata satisfies and .
Hint
Note that the input is not necessarily uniformly distributed in the interval , so the total error may be very large.
Translated by ChatGPT 5