QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 128 MB Points totaux : 100

#16303. Weather forecast

Statistiques

Bajtek is just beginning his adventure with artificial intelligence. He has decided to test his newly acquired knowledge on a weather forecasting problem. The goal is to output a sequence of $m$ integers $p_1, p_2, \dots, p_m$ that describe a temperature forecast for the coming days. Bajtek produced such a sequence some time ago and is now wondering how good his forecast was. To this end, he downloaded a sequence of $n$ integers $t_1, t_2, \dots, t_n$ from the internet (where $m \le n$), which describes the actual temperatures on consecutive days. He would now like to choose a contiguous fragment of length $m$ in such a way that the number of discrepancies with the forecast is as small as possible. Since the results of preliminary experiments turned out to be somewhat unsatisfactory, Bajtek may first cyclically rotate his forecast and only then compare it with the data.

More formally, Bajtek first chooses a cyclic rotation of his forecast, i.e., the sequence $p_i, p_{i+1}, \dots, p_m, p_1, \dots, p_{i-1}$, for any chosen $1 \le i \le m$. Let us denote the rotated forecast by $p'_1, p'_2, \dots, p'_m$. Next, he applies the rotated forecast to a chosen position $j$ in the data sequence, for any chosen $1 \le j \le n - m + 1$. Finally, he calculates the number of discrepancies, i.e., the number of positions $k$ (for $1 \le k \le m$) such that $p'_k \neq t_{j+k-1}$. He would like to do this in such a way that the number of discrepancies is as small as possible. Help him determine this minimum number!

Input

The first line of input contains two integers $n$ and $m$ ($1 \le m \le n \le 10\,000$) denoting the length of the data sequence and the length of the forecast sequence, respectively. The second line contains a sequence describing the actual data, consisting of $n$ integers $t_1, t_2, \dots, t_n$ ($-20 \le t_i \le 40$). The third line contains a sequence describing the forecast generated by Bajtek, consisting of $m$ integers $p_1, p_2, \dots, p_m$ ($-20 \le p_i \le 40$).

Output

The first and only line of output should contain a single integer representing the minimum possible number of discrepancies when applying some cyclic rotation of the forecast to the data.

Examples

Input 1

5 3
-1 2 0 3 0
2 3 -1

Output 1

1

Note

Explanation of the example: Bajtek can cyclically rotate the forecast to obtain the sequence $-1, 2, 3$, which he can then apply to the fragment of data starting at the first position (i.e., $-1, 2, 0$). In this case, they differ only at the third position, so the number of discrepancies is $1$. We can also see that none of the sequences $2, 3, -1$ (rotation from $i=1$), $3, -1, 2$ (rotation from $i=2$), and $-1, 2, 3$ (rotation from $i=3$) is a contiguous fragment of the data, so it is not possible to obtain a smaller number of discrepancies.

Sample tests

Test 0a is the test from the example above. Additionally:

0b: $n = 1000, m = 500$ and $t_i = (i \pmod{61}) - 20$ for $1 \le i \le n$ and $p_i = ((i + 13) \pmod{61}) - 20$ for $1 \le i \le m$. 0c: $n = 3000, m = 2000$ and the first 1500 elements of sequence $t$ are $0$, and the next 1500 are $1$, while the first 1000 elements of sequence $p$ are $1$, and the next 1000 are $0$. 0d: $n = 3500, m = 2000$ and sequence $t$ consists of seven contiguous fragments of length 500 each with values $6, 5, 4, 3, 2, 1, 0$ in that order, while sequence $p$ satisfies $p_i = i \pmod 7$ for $1 \le i \le m$. 0e: $n = 5000, m = 3000$ and $t_i = 4$ for $1 \le i \le n$, while $p_i = 4$ for $1 \le i < m$ and $p_m = 5$. 0f: $n = 10\,000, m = 10\,000$ and $t_i = i \pmod 3$ for $1 \le i \le n$, while $p_i = i \pmod 4$ for $1 \le i \le m$.

Subtasks

Subtask Constraints Points
1 $n \le 1000$ 12
2 $n \le 3000$ and values of data and forecast are in the range $0$ to $1$ 19
3 $n \le 3500$ and the sequence $t_i$ is non-increasing 15
4 $n \le 3000$ 22
5 $n \le 5000$ 15
6 no additional constraints 17

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.