A - Chess Placing CodeForces - 985A

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW".

Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to .

In one step you can move one of the pieces one cell to the left or to the right. You cannot move pieces beyond the borders of the board. You also cannot move pieces to the cells that are already occupied.

Your task is to place all the pieces in the cells of the same color using the minimum number of moves(all the pieces must occupy only the black cells or only the white cells after all the moves are made).

Input

The first line of the input contains one integer n (2 ≤ n ≤ 100, n is even) — the size of the chessboard.

The second line of the input contains  integer numbers  (1 ≤ pi ≤ n) — initial positions of the pieces. It is guaranteed that all the positions are distinct.

Output

Print one integer — the minimum number of moves you have to make to place all the pieces in the cells of the same color.

Examples

Input
6
1 2 6
Output
2
Input
10
1 2 3 4 5
Output
10

Note

In the first example the only possible strategy is to move the piece at the position 6 to the position 5 and move the piece at the position 2 to the position 3. Notice that if you decide to place the pieces in the white cells the minimum number of moves will be 3.

In the second example the possible strategy is to move  in 4 moves, then  in 3 moves, in 2 moves and  in 1 move.

题目分析:

题目的意思就是让我们把n/2个数全部变成奇数或者全部变成偶数的最小步数,分别变成奇数和分别变成偶数,取最小值就好了.

还有要注意的就是,虽然题目说You cannot move pieces beyond the borders of the board但是实际上这个对我们求最小步数是不会影响的,我们可以手动验证.

比如输入的是

8

3 4 6 7

假设我们把他们全部变成奇数,则有以下步骤:

1 4 6 7  move=2

1 3 6 7 move=3

1 3 5 7  move=4;

我们也可以直接把4移到1,6移到5,move=4

所以,我们要求的move,我们只要把所有的数sort,然后第i个数字对应的最近的奇数就是2*i-1,move+=abs(2*i-1)-num[i],偶数也同样的做法.

所以ac代码如下:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n,num[105];
 9     cin>>n;
10     for(int i=1;i<=n/2;i++)
11     {
12         cin>>num[i];
13     }
14 
15     sort(num+1,num+n/2+1);
16 
17     int ans1=0,ans2=0;
18 
19     for(int i=1;i<=n/2;i++)
20     {
21         ans1+=abs(2*i-1-num[i]);//奇数的move
22         ans2+=abs(2*i-num[i]);//偶数的move
23     }
24     cout<<min(ans1,ans2)<<endl;
25 }
View Code

  

猜你喜欢

转载自www.cnblogs.com/Lewin671/p/9078976.html