atcoder#AGC058E. [AGC058E] Nearer Permutation
[AGC058E] Nearer Permutation
Score : points
Problem Statement
In this problem, a "permutation" always means a permutation of .
For two permutations and , let us define the distance between them, , as follows.
- Consider making equal by repeatedly swapping two adjacent terms in . Let be the minimum number of swaps required here.
Additionally, for a permutation , let us define another permutation as follows.
- Let . Consider permutations such that . Let be the lexicographically smallest of those permutations.
For example, when , the permutations such that are . The lexicographically smallest of them is , so we have .
You are given a permutation . Determine whether there is a permutation such that .
Solve test cases for each input file.
What is lexicographical order on sequences?
The algorithm described here determines the lexicographical order between distinct sequences and .
Below, let denote the -th element of . Additionally, let and mean " is lexicographical smaller than " and " is lexicographical larger than ," respectively.
- Let be the length of the shorter of and . For each , let us check whether and are equal.
- If there is such that , let be the smallest such , and compare and . If is smaller than (as a number), we conclude and exit; if is larger than , we conclude and exit.
- If there is no such that , compare the lengths of and . If is shorter than , we conclude and exit; if is longer than , we conclude and exit.
Constraints
- is a permutation of .
- The sum of in one input file does not exceed .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Each case is in the following format:
Output
For each case, print Yes
if there is a permutation such that , and No
otherwise.
2
2
1 2
2
2 1
Yes
Yes
For instance, when , one can let to satisfy .
6
3
1 2 3
3
1 3 2
3
2 1 3
3
2 3 1
3
3 1 2
3
3 2 1
Yes
Yes
Yes
Yes
No
No
For instance, when , one can let to satisfy .
24
4
1 2 3 4
4
1 2 4 3
4
1 3 2 4
4
1 3 4 2
4
1 4 2 3
4
1 4 3 2
4
2 1 3 4
4
2 1 4 3
4
2 3 1 4
4
2 3 4 1
4
2 4 1 3
4
2 4 3 1
4
3 1 2 4
4
3 1 4 2
4
3 2 1 4
4
3 2 4 1
4
3 4 1 2
4
3 4 2 1
4
4 1 2 3
4
4 1 3 2
4
4 2 1 3
4
4 2 3 1
4
4 3 1 2
4
4 3 2 1
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No