#1294. Problem A. 逆序对染色
Problem A. 逆序对染色
We know the definition of reverse pairs: for a permutation of length , if there exists and , then form a reverse pair. A permutation of length contains each of the numbers to exactly once.
Finding the number of reverse pairs should not be a difficult task for you. Now, you are given a permutation of length . For each pair satisfying and of reverse pairs, color the cell in the grid at row and column . Please determine how many colored connected components are there in the final grid. Connected means that if two colored cells share an edge, then we consider these two cells to be connected.
Input
The first line contains an integer (), representing the length of the permutation.
The second line contains integers , representing a permutation of length .
Output
Output an integer, representing the number of four-connected components in the final grid.
Examples
9
5 9 1 8 2 6 4 7 3
5
2
1 2
0