Study about Since lexicographical orders and combinations often appear, let’s master this opportunity. It seems that it is also called permutation full search.

I will understand the mathematical lexicographical order, permutation, and the explanation of combination ~~ troublesome ~ ~ I will omit it, so let’s omit it.

This time I will think about various things while solving problems.
I’ll search for past questions and solve problems like that.

Click here for question texts/input examples, etc. *Please refer to the problem link as much as possible

- --
(Start of section)
【Problem statement】
There are permutations of size N (a sequence of (1, 2, ..., N) permuted) P, Q.
There are N! possible permutations of size N. Find |a−b|, where P is the lexicographically the ath smallest and Q is the lexicographically the bth smallest.
[Note]
For two sequences X, Y, X is defined to be lexicographically less than Y when some integer k exists and Xi=Yi (1 ≤ i<k) and Xk<Yk.
[Restrictions]
- 2 ≤ N ≤ 8
- P and Q are permutations of size N.
- All inputs are integers.
【input】
Input is given from standard input in the following format:
```
N
P1 P2 ... PN
Q1 Q2 ... QN
```
【output】
Print |a−b|.
(End of section)
- --

I had such a problem. The question is lexicographical order.
Since it is a C problem and there are not so many restrictions, it seems that it may be possible to solve it without thinking carefully if it goes simply, but let’s think about it for a moment.

Let’s take a look at one input example and one output example.

This article introduces the easy-to-use and highly configurable Java library “Java Cloud LOG Java Producer” that supports sending data to a log service.

What is an expression?
public class Main {
public static void main(String[] args) {
int a;
int b;
a = 20;
b =a+5;
System.out.println(a);
System.out.println(...