No.696 square1001 and Permutation 5
タグ : / 解いたユーザー数 20
作問者 :
Pulmn
Problem Statement
In the year 2168, John and Brus participated in the JOI, and they both solved every problem in the final round and won. So it was expected that John and Brus would place in the top 4 at the JOI Spring Camp and become members of the Japanese IOI team.
Unfortunately, however, both of them failed at the Spring Camp and tied for 4th place with the same score. The JOI Spring Camp lasts 4 days, and they had the same score on all 4 days. Since they had also both solved every problem in the final round and had the same score there, it was decided that the representative would be determined by a "Final Duel"!
In the Final Duel, the following problem was given. John and Brus tried to solve this problem, staking their last hope on it.
- You are given a permutation $p = (p_1, p_2, p_3, \cdots, p_N)$ of $1, 2, 3, \cdots, N$. What is the position of $p$ in lexicographical order among the permutations of $1, 2, 3, \cdots, N$?
John and Brus figured out how to solve this problem, implemented it, and fixed the bugs in their code. Two hours passed, and both of them solved the problem correctly. However, John had submitted just 1 second earlier than Brus!
Therefore, John was chosen as the representative of Japan. Poor Brus was unfortunately not chosen for the Japanese IOI team, and he was even sent to prison!
You have heard the story of John and Brus. Now, solve this problem within 2 hours and prove that you are more skilled than they are!
Source of the problem statement: SRM 719 Div1 Hard - RedemptionOfMatthew99
Input
$N$ $p_1 \ p_2 \ p_3 \ \cdots \ p_N$
- The $1$ st line contains an integer $N$.
- The $2$ nd line contains $N$ integers $p_1, p_2, p_3, \cdots, p_N$ separated by spaces.
Output
Output on $1$ line the position of $p$ in lexicographical order among the permutations of $1, 2, 3, \cdots, N$.
Constraints
All input data satisfy the following constraints.
- $1 \le N \le 100 \ 000$.
- $1 \leq p_i \leq N \ (1 \leq i \leq N)$
- $p_i \neq p_j \ (1 \leq i < j \leq N)$
Sample
Sample 1
Input
3 2 3 1
Output
4
The permutations of $1, 2, 3$ that are lexicographically smaller than this one are $(1, 2, 3), (1, 3, 2), (2, 1, 3)$, which number $3$, so $(2, 3, 1)$ is the $4$ th in lexicographical order.
Sample 2
Input
5 3 1 5 4 2
Output
54
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。