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
(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 3 moves,
in 2 moves and
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 ...
随机推荐
- Java Redis Pipeline 使用示例
1. 参考的优秀文章 Request/Response protocols and RTT 2. 来源 原来,系统中一个树结构的数据来源是Redis,由于数据增多.业务复杂,查询速度并不快.究其原因, ...
- delete 与 delete []的区别
一. 针对类class,这两种方式的效果是不同的. 当你通过下列方式分配一个类对象数组: class A { private: char *m_cBuffer; int ...
- cylance做的机器学习相关材料汇总
https://www.cylance.com/en_us/products/our-products/protect----threatzero.html 产品介绍 关键!!!! https://w ...
- 三个大数据处理框架:Storm,Spark和Samza 介绍比较
转自:http://www.open-open.com/lib/view/open1426065900123.html 许多分布式计算系统都可以实时或接近实时地处理大数据流.本文将对三种Apache框 ...
- Mongodb 的劣势
MongoDB中的数据存放具有相当的随意性,不具有MySQL在开始就定义好了.对运维人员来说,他们可能不清楚数据库内部数据的数据格式,这也会数据库的运维带来了麻烦. 1. 事务关系支持薄弱.这也是所有 ...
- Windows10使用Chocolatey安装mysql之后无法使用的解决办法
问题背景:使用了一台新的虚拟机,并且安装了Chocolatey作为Windows的包管理器,之后安装mysql 那么问题发生了,使用mysql命令根本没有任何反应,也不报错,但是安装的时候是提示安装成 ...
- Raspberry Pi 配置
资料 教程 <树莓派使用指南> 系统盘制作 插上电源,连上HDML线,计科
- 无线网破解 跑字典 EWSA使用教程
当我们用奶瓶抓到包,就可以再windowsxp&7&8下跑字典 工具/原料 EWSA4.0完美汉化破解版 字典 方法/步骤 1.打开程序 2.导入握手包 3.配置EWSA ,1.选项 ...
- 因实现本地浏览器访问nginx修改配置文件后,安装vsftpd失败
解决方法如下(修改dns配置) vi /etc/resolv.conf 在此文件最后加入:nameserver 8.8.8.8 如果没有vi编辑器可用: echo "nameserver 8 ...
- CentOS虚拟机中安装VMWare Tools
1.单击VMWare的[虚拟机]菜单,选择[安装VMWare Tools]命令 2.接着CentOS系统会自动挂载VMWare Tools,并自动打开,如果没有打开可以自己去图形界面打开VMWare ...