A. Chess Placing

Codeforces
IDCF985A
Time1000ms
Memory256MB
Difficulty
implementation
English · Original
Chinese · Translation
Formal · Original
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 ≤ _p__i_ ≤ _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. [samples] ## 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.
你被给定一个大小为 #cf_span[1 × n] 的棋盘。保证 *#cf_span[n] 是偶数*。该棋盘的染色方式如下:"_BWBW_#cf_span[...]_BW_"。 棋盘的某些格子上放置了棋子。每个格子至多包含一个棋子。已知棋子的总数为 。 在一步操作中,你可以将任意一个棋子向左或向右移动一格。你不能将棋子移出棋盘边界,也不能将棋子移动到已被占据的格子上。 你的任务是通过最少的移动次数,将所有棋子放置在同一种颜色的格子上(所有棋子最终必须全部位于黑色格子上,或全部位于白色格子上)。 输入的第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 100], *#cf_span[n] 是偶数*) —— 棋盘的大小。 第二行包含 个整数 (#cf_span[1 ≤ pi ≤ n]) —— 棋子的初始位置。保证所有位置互不相同。 请输出一个整数 —— 将所有棋子放置在同一种颜色格子上所需的最少移动次数。 在第一个例子中,唯一可能的策略是将位置 #cf_span[6] 的棋子移动到位置 #cf_span[5],并将位置 #cf_span[2] 的棋子移动到位置 #cf_span[3]。注意,如果你选择将棋子全部放置在白色格子上,最少移动次数将是 #cf_span[3]。 在第二个例子中,一种可能的策略是:在 4 步内移动 ,然后在 3 步内移动 ,在 2 步内移动 ,在 1 步内移动 。 ## Input 输入的第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 100], *#cf_span[n] 是偶数*) —— 棋盘的大小。第二行包含 个整数 (#cf_span[1 ≤ pi ≤ n]) —— 棋子的初始位置。保证所有位置互不相同。 ## Output 请输出一个整数 —— 将所有棋子放置在同一种颜色格子上所需的最少移动次数。 [samples] ## Note 在第一个例子中,唯一可能的策略是将位置 #cf_span[6] 的棋子移动到位置 #cf_span[5],并将位置 #cf_span[2] 的棋子移动到位置 #cf_span[3]。注意,如果你选择将棋子全部放置在白色格子上,最少移动次数将是 #cf_span[3]。在第二个例子中,一种可能的策略是:在 4 步内移动 ,然后在 3 步内移动 ,在 2 步内移动 ,在 1 步内移动 。
Let $ n $ be an even positive integer, and let $ P = \{p_1, p_2, \dots, p_k\} $ be a set of $ k $ distinct initial positions of chess pieces on a $ 1 \times n $ board, where $ 1 \leq p_i \leq n $. The board is colored in alternating black and white starting with black at position 1: - Cell $ i $ is **black** if $ i $ is odd. - Cell $ i $ is **white** if $ i $ is even. Let $ B = \{ i \in [1, n] \mid i \text{ odd} \} $ be the set of black cells. Let $ W = \{ i \in [1, n] \mid i \text{ even} \} $ be the set of white cells. We are to move all pieces to occupy **only black cells** or **only white cells**, with minimum total moves, where each move shifts a piece one cell left or right, without overlapping or going beyond bounds. Define: - $ \text{cost}_B $: minimum total moves to move all pieces to distinct black cells. - $ \text{cost}_W $: minimum total moves to move all pieces to distinct white cells. Then the answer is: $$ \min(\text{cost}_B, \text{cost}_W) $$ To compute $ \text{cost}_B $: - Sort the initial positions: $ p_1' \leq p_2' \leq \dots \leq p_k' $. - Sort the black cell positions: $ b_1 < b_2 < \dots < b_{n/2} $. - Assign the $ i $-th piece (in sorted order) to the $ i $-th black cell (in sorted order) — this minimizes total distance due to the rearrangement inequality. - $ \text{cost}_B = \sum_{i=1}^k |p_i' - b_i| $ Similarly, to compute $ \text{cost}_W $: - Sort the white cell positions: $ w_1 < w_2 < \dots < w_{n/2} $. - $ \text{cost}_W = \sum_{i=1}^k |p_i' - w_i| $ Output: $$ \min\left( \sum_{i=1}^k |p_i' - b_i|,\ \sum_{i=1}^k |p_i' - w_i| \right) $$
Samples
Input #1
6
1 2 6
Output #1
2
Input #2
10
1 2 3 4 5
Output #2
10
API Response (JSON)
{
  "problem": {
    "name": "A. Chess Placing",
    "description": {
      "content": "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",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF985A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a chessboard of size 1 × _n_. It is guaranteed that **_n_ is even**. The chessboard is painted like this: \"_BWBW_..._BW_\".\n\nSome cells of the board are occupied by the chess pieces. Each...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定一个大小为 #cf_span[1 × n] 的棋盘。保证 *#cf_span[n] 是偶数*。该棋盘的染色方式如下:\"_BWBW_#cf_span[...]_BW_\"。\n\n棋盘的某些格子上放置了棋子。每个格子至多包含一个棋子。已知棋子的总数为 。\n\n在一步操作中,你可以将任意一个棋子向左或向右移动一格。你不能将棋子移出棋盘边界,也不能将棋子移动到已被占据的格子上。\n\n你的任务是通过最少的移...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be an even positive integer, and let $ P = \\{p_1, p_2, \\dots, p_k\\} $ be a set of $ k $ distinct initial positions of chess pieces on a $ 1 \\times n $ board, where $ 1 \\leq p_i \\leq n $.\n\nTh...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments