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 ...
随机推荐
- SpringCloud之eureka服务注册和服务发现
服务注册中心 :eureka-server 作用:服务注册中心提供服务注册功能 服务提供方:eureka-client 作用:注册服务到服务注册中心 服务注册中心 :eureka-server 创建 ...
- JNI_Z_10_Java的数组
在Java中数组分为两种: (1).基本类型数组 (2).对象类型(Object[])的数组 (数组中存放的是指向Java对象中的引用) 一个能通用于两种不同类型数组的函数: GetArrayLeng ...
- spring boot MongoDB的集成和使用
前言 上一章节,简单讲解了如何集成Spring-data-jpa.本章节,我们来看看如何集成NoSQL的Mongodb.mongodb是最早热门非关系数据库的之一,使用也比较普遍.最适合来存储一些非结 ...
- 基于mysql的全文索引
支持引擎:mysql的MyISAM存储引擎和Innodb存储引擎(5.6及其以上)支持. 适用类型:char.varchar和text. 新建方法:ALTER TABLE article ADD F ...
- Python中threading模块的join函数
Join的作用是阻塞进程直到线程执行完毕.通用的做法是我们启动一批线程,最后join这些线程结束,例如: for i in range(10): t = ThreadTest(i) thread_ar ...
- 013——VUE中多种方式使用VUE控制style样式属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- 启动代码之开iCache
1.什么是cache,有什么用 cache是一种内存,叫高速缓存.从容量来说:CPU < 寄存器 < cache < DDR从速度来说:CPU > 寄存器 > ca ...
- git远程分支回退
[本地代码回退] git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除 git reset --hard HEAD~3:将 ...
- MySqlDBHelper数据库连接
这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...