atcoder#ARC160A. [ARC160A] Reverse and Count
[ARC160A] Reverse and Count
Score : points
Problem Statement
You are given a permutation of . For a pair of integers such that , let be the permutation obtained by reversing the -th through -th elements of , that is, replacing with simultaneously.
There are ways to choose such that . If the permutations for all such pairs are listed and sorted in lexicographical order, what is the -th permutation from the front?
What is lexicographical order on sequences?
A sequence is said to be lexicographically smaller than a sequence if and only if 1. or 2. below holds. Here, and denote the lengths of and , respectively.
- and $(S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})$.
- There is an integer that satisfies both of the following.
- $(S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})$.
- is smaller than (as a number).
Constraints
- is a permutation of .
Input
The input is given from Standard Input in the following format:
Output
Let be the -th permutation from the front in the list of the permutations sorted in lexicographical order. Print in the following format:
3 5
1 3 2
2 3 1
Here are the permutations for all pairs such that .
When these are sorted in lexicographical order, the fifth permutation is , which should be printed.
5 15
1 2 3 4 5
5 4 3 2 1
The answer is .
10 37
9 2 1 3 8 7 10 4 5 6
9 2 1 6 5 4 10 7 8 3