codeforce 985A Chess Placing(暴力)
1 second
256 megabytes
standard input
standard output
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).
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
 integer numbers  (1 ≤ pi ≤ n) — initial positions of the pieces. It is guaranteed that all the positions are distinct.
 (1 ≤ pi ≤ n) — initial positions of the pieces. It is guaranteed that all the positions are distinct.
Print one integer — the minimum number of moves you have to make to place all the pieces in the cells of the same color.
6
1 2 6
2
10
1 2 3 4 5
10
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 4 moves, then  in 3 moves,
 in 3 moves,  in 2 moves and
 in 2 moves and  in 1 move.
 in 1 move.
两种情况分别跑一遍,求最少
#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
int main()
{
int n;
std::cin>>n;
int a[];
for(int i=;i<=n/;i++)
{
std::cin>>a[i];
}
sort(a+,a++n/);
int s1=;
int s2=;
int c1=;
int c2=;
for(int i=;i<=n/;i++,c1+=,c2+=)
{
s1+=abs(a[i]-c1);
s2+=abs(a[i]-c2);
}
s1=min(s2,s1);
std::cout<<s1;
return ;
}
codeforce 985A Chess Placing(暴力)的更多相关文章
- codeforces 985A Chess Placing
		题意: 移动最少的步数,使得所有的棋子在同一颜色的格子中. 每次一个棋子只能向左或者向右移动一步,不能移到有棋子的格子中. 思路: 枚举全黑和全白的情况. 对于每一个需要移动的棋子,它移动到的位置一定 ... 
- 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 ... 
- codeforce Number of Ways(暴力)
		#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ... 
- A. Chess Placing
		链接 [https://codeforces.com/contest/985/problem/A] 题意 给你一个偶数n,输入n/2个数,代表棋子的位置,有一个1*n的棋盘是黑白相间的 问你使得所有棋 ... 
- CF985A Chess Placing【思维】
		[链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ... 
- Educational Codeforces Round 44 (Rated for Div. 2)
		题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ... 
- 逆向暴力求解 538.D Weird Chess
		11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ... 
- CodeForce 855B 暴力or线段树
		CodeForce 855B 暴力or线段树 题意 给你一串数,然后找出三个数,他们的前后关系和原来一样,可以相同,然后分别乘p,q,r,求他们积的和最大,并且输出这个数. 解题思路 这个可以使用线段 ... 
- codeforce 429D. Tricky Function (思维暴力过)
		题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ... 
随机推荐
- git常用操作 配置用户信息、拉取项目、提交代码、分支操作、版本回退...
			git常用操作 配置用户信息.拉取项目.提交代码.分支操作.版本回退... /********git 配置用户信息************/ git config --global user.name ... 
- springmvc学习(2)
			SpringMvc是最主流的MVC框架之一. Spring3.0后全面超越Struts2,成为最优先的MVC框架 SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,而无须实现任何借 ... 
- appium自动化测试(一)
			一. appium的引入 二. adb adb(Android Debug Brige)是用来连接安卓手机和PC端的调试桥梁,通过adb服务,在PC端命令行界面对手机或者模拟器进行全面的操作 安装: ... 
- ubuntu 编译 vim+lua
			mac $ brew install vim --with-cscope --with-lua --override-system-vim 安装spf13-vim见下面linux部分. 如果安装mac ... 
- 转:大数据时代快速SQL引擎-Impala
			本文来自:http://blog.csdn.net/yu616568/article/details/52431835 如有侵权 可立即删除 背景 随着大数据时代的到来,Hadoop在过去几年以接近统 ... 
- Spring 依赖注入(一、注入方式)
			Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ... 
- webpack2配置
			详细的配置可以参考官网:https://doc.webpack-china.org/guides/ 一开始做项目时都是直接从组里前辈搭建好的脚手架开始写代码,到后来自己写新项目时又是拷贝之前的工程作为 ... 
- Java中的深拷贝(深复制)和浅拷贝(浅复制)
			深拷贝(深复制)和浅拷贝(浅复制)是两个比较通用的概念,尤其在C++语言中,若不弄懂,则会在delete的时候出问题,但是我们在这幸好用的是Java.虽然java自动管理对象的回收,但对于深拷贝(深复 ... 
- Drools7.0.0.Final Unsupported major.minor version 52.0异常
			异常信息 在使用Drools7.0.0.Final版本进行开发过程中,出现以下异常: java.lang.UnsupportedClassVersionError: org/kie/api/KieSe ... 
- mac和linux下使用Docker,部署SpringBoot项目到docker
			主要是看一下如何在linux及mac上安装docker,创建docker镜像,部署SpringBoot项目到docker,并借助于DaoCloud进行docker镜像下载加速等. 我用的电脑是mac, ... 
