GREEDY, IMPLEMENTATION, SORTING
Given a sequence of integers
A1, A2, ..., AN, count the minimum number of moves needed to build a permutation, provided in one move, you are allowed to decrease or increase any number by one
The solution of the problem is rather simple. Sort all integers a and then make
integer 1 from a,
integer 2 from a and so on.
a[i] adds to the answer the value
|a[i] - i|. The answer should be count in 64-bit type. You can simply guess why such solution is correct.
Author’s solution can be found here.