A. Zhily and Array Operating
Since each element can only be selected at most once, thus we can start by accumulating the suffix unless the suffix sum is negative, and add the suffix to current element.
Time complexity .
AC Code
1 |
|
B
We first count the global max and mex. We can divide into several cases.
- If
max == 0, then all elements are , the answer is . - If
max == 1, then if there exists , the answer is , otherwise, it’s . - Then if
mex > max, this means1,2, ..., maxare in the array, we first putmaxat first, then1,2,..max-1after it. This is the optimal - If
mex < max, this arrangement is also optimal.
C
We can adopt a greedy solution. If , then swapping makes no sense; otherwise, let’s assume , if has more than , then we swap .
After processing all positions, we check if are valid.
D
A common expansion of expectation is to exploit its linearity to convert counting problem into independent judgement problem. For this problem, expected number of inversion pairs is equivalent to
Since are fixed and are arbitrary, we can use a approach to count how many pairs of satisfies this. We collect all , sort them, and for each pair of , we binary search in the sorted array to count.