赞
踩
https://www.lucien.ink/archives/232/
http://codeforces.com/contest/985/problem/A
You are given a chessboard of size 1 × n
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 n2
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).
The first line of the input contains one integer n
The second line of the input contains integer numbers (1 ≤ pi ≤ n)
Print one integer — the minimum number of moves you have to make to place all the pieces in the cells of the same color.
有一个1×n
枚举一遍全挪到黑色再枚举一遍全挪到白色取最小值即可。
#include <bits/stdc++.h>
int main() {
int n, p[1007], sum[2] = {0}, cur[2] = {1, 2};
std::cin >> n;
for (int i = 1; i <= n / 2; i++) std::cin >> p[i];
std::sort(p + 1, p + 1 + n / 2);
for (int i = 1; i <= n / 2; i++, cur[0] += 2, cur[1] += 2) {
sum[0] += abs(cur[0] - p[i]);
sum[1] += abs(cur[1] - p[i]);
}
std::cout << std::min(sum[0], sum[1]) << std::endl;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。