#P8087. 『JROI-5』Interval
『JROI-5』Interval
Background
Little C likes data structures with interval operations, because such problems always have an easy-to-write partial score.
Problem Description
This problem has a large input size. It is recommended to use a fast input method. You may refer to the contest bulletin board.
Little C has a sequence of length , where the -th term is .
is a permutation of (that is, each of appears exactly once in ).
Define as the smallest positive integer that does not appear in .
For example, , and .
Little C also has an array of length .
An interval is defined to be valid if and only if
Little C wants you to tell him the length of the shortest valid interval. In particular, if no interval is valid, output 0.
Input Format
The first line contains a positive integer .
The second line contains positive integers .
The third line contains positive integers .
Output Format
Output one integer in a single line, indicating the length of the shortest valid interval.
5
2 3 1 5 4
2 2 3 4 5
3
5
2 3 1 5 4
1 2 2 4 5
1
5
1 3 4 2 5
6 7 8 9 10
0
见附件
见附件
Hint
[Sample Explanation]
For #1, it is easy to see that is the shortest valid interval.
For #2, it is easy to see that is the shortest valid interval.
For #3, it is easy to see that there is no valid interval.
For of the testdata, .
For of the testdata, .
For another of the testdata, is non-increasing, i.e. , and .
For of the testdata, and .
Translated by ChatGPT 5