285C.md (1495B)
1 Source: https://codeforces.com/problemset/problem/285/C 2 3 C. Building Permutation 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Permutation *p* is an ordered set of integers *p*1, *p*2, ..., *p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1, *p*2, ..., *p**n*. 22 23 You have a sequence of integers *a*1, *a*2, ..., *a**n*. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence. 24 25 Input 26 27 The first line contains integer *n* (1 ≤ *n* ≤ 3·105) — the size of the sought permutation. The second line contains *n* integers *a*1, *a*2, ..., *a**n* ( - 109 ≤ *a**i* ≤ 109). 28 29 Output 30 31 Print a single number — the minimum number of moves. 32 33 Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. 34 35 Examples 36 37 Input 38 39 ``` 40 2 41 3 0 42 ``` 43 44 Output 45 46 ``` 47 2 48 ``` 49 50 Input 51 52 ``` 53 3 54 -1 -1 2 55 ``` 56 57 Output 58 59 ``` 60 6 61 ``` 62 63 Note 64 65 In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1). 66 67 In the second sample you need 6 moves to build permutation (1, 3, 2).