Problem 508
Minimum Increments to Make Array Unique
Medium · 20 pts · Array, Greedy, Sorting
Problem
In one move you may increase any element by 1. Find the minimum number of moves needed to make all elements of the array different. Elements can only go up, never down. Sort the array; then each element must be at least one more than the element placed before it, so raise it to max(itself, previous + 1) and add the difference to the total.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the minimum number of moves.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 100000 (the answer fits in a 64-bit integer)
Example input
6 3 2 1 2 1 7
Example output
6
Explanation
Sorted, 3 2 1 2 1 7 is 1 1 2 2 3 7. Raising it to 1 2 3 4 5 7 takes 0 + 1 + 1 + 2 + 2 + 0 = 6 moves, and every value is then different.