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 ...
随机推荐
- U盘安装XP
ZC: 第1阶段结束 进入第2阶段 的时候,还是需要手动选择USB启动 (如果没有修改BIOS为USB启动的话) ZC: 我用的 WinSetupFromUSB,是文章尾部的那个版本的软件界面. ZC ...
- Win7.还原默认打开方式
1.win7还原默认打开方式_百度知道.html(https://zhidao.baidu.com/question/1668708948433912307.html) Windows7:[47]打开 ...
- 测绘类SCI
GeoInformatica(国际地理信息系统计算机科学进展杂志)美国International Journal of Geographical Information Science(国际地理信息科 ...
- angular-schema-form 自动表单生成
基本用法 通过bower安装之后,将schemaForm模块载入到模块定义中,fuse中安装第三方包需要在app/core/core.module.js中声明. 然后在controller里面,将sc ...
- 浅谈 session 会话的原理
先谈 cookie 网络传输基于的Http协议,是无状态的协议,即每次连接断开后再去连接,服务器是无法判断此次连接的客户端是谁. 如果每次数据传输都需要进行连接和断开,那造成的开销是很巨大的. 为了解 ...
- python函数式编程之高阶函数学习
基本概念 函数式编程,是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量.因此,任意一个函数,只要输入确定,输出就确定的这种函数我们称之为纯函数,我们称这种函数没有副作用.而允许使用 ...
- lightoj1197区间素数筛
模板题,不过好像有点问题,当a==1的时候,答案把一也算进去了,要减去 #include<map> #include<set> #include<cmath> #i ...
- IOS-SQLite数据库使用详解
使用SQLite数据库 创建数据库 创建数据库过程需要3个步骤: 1.使用sqlite3_open函数打开数据库: 2.使用sqlite3_exec函数执行Create Table语句,创建数据库表: ...
- Python正则表达式------进阶
Python正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re ...
- Quartz快速入门
Quartz是一个定时任务调度的框架,在预定的时间到达时,执行某一任务 可认为是JDK的Timer类的扩展 Quartz的核心接口Scheduler – 核心调度器Job – 任务JobDetail ...