Official algorithmic problem description and constraints.
You are given an array A
A of size N
N.
The score of a permutation P
P of the integers [1,N]
[1,N] is defined as follows:
Find the maximum possible score for some permutation P
P. You also need to print a valid permutation.
For each test case, output the following:
If multiple answers are possible, all will be accepted.
Input
Output
3 3 1 1 1 3 3 1 2 2 2 2 3 2 3 1 2 3 1 2 2 2 1
Test Case 1 : Any permutation P
P which does not have Pi=i
Pi
=i for any index i
i, would have a score of 3
3 because all values are 1
1, so APi≤Ai
APi
≤Ai
always holds.
Test Case 2 : P=[3,1,2]
P=[3,1,2] means that index i=1,3
i=1,3 are good because AP1=A3=2≤A1
AP1
=A3
=2≤A1
, and AP3=A2=1≤A3
AP3
=A2
=1≤A3
. It can be shown that a larger score is impossible.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.