题意:

移动最少的步数,使得所有的棋子在同一颜色的格子中。

每次一个棋子只能向左或者向右移动一步,不能移到有棋子的格子中。

思路:

枚举全黑和全白的情况。

对于每一个需要移动的棋子,它移动到的位置一定是从1开始第一个可以移动的位置,不交叉移动,保证了步数最小。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = ;
int v[N],g[N];
int mabs(int x)
{
return x >= ? x : -x;
}
int main()
{
int n;
scanf("%d",&n);
for (int i = ;i < n / ;i++)
{
int x;
scanf("%d",&x);
v[x] = g[x] = ;
}
int ans = ;
for (int i = ;i <= n;i++)
{
int ma = -;
if (i % && g[i])
{
for (int j = ;j <= n;j++)
{
if (j % == && !g[j])
{
ma = j;
break;
}
}
ans += mabs(ma - i);
g[ma] = ;
g[i] = ;
}
}
int cnt = ;
for (int i = ;i <= n;i++)
{
int ma = -;
if (i % == && v[i])
{
for (int j = ;j <= n;j++)
{
if (j % && !v[j])
{
ma = j;
break;
}
}
cnt += mabs(ma - i);
v[ma] = ;
v[i] = ;
}
}
printf("%d\n",min(ans,cnt));
return ;
}

codeforces 985A Chess Placing的更多相关文章

  1. codeforce 985A Chess Placing(暴力)

    Chess Placing time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 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 ...

  3. codeforces 893A Chess For Three 模拟

    893A Chess For Three 思路: 直接模拟即可,第一盘永远是A与B开始 代码: #include <bits/stdc++.h> using namespace std; ...

  4. A. Chess Placing

    链接 [https://codeforces.com/contest/985/problem/A] 题意 给你一个偶数n,输入n/2个数,代表棋子的位置,有一个1*n的棋盘是黑白相间的 问你使得所有棋 ...

  5. Codeforces 38B - Chess

    38B - Chess 思路:懂点象棋的规则就可以,看看哪些点可以放马. 代码: #include<bits/stdc++.h> using namespace std; #define ...

  6. codeforces 845A Chess Tourney

    参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...

  7. CF985A Chess Placing【思维】

    [链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ...

  8. Codeforces 845A. Chess Tourney 思路:简单逻辑题

    题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力.    实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢.    叫你把这2*n个选手分成2个有n个选手的队伍. ...

  9. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

随机推荐

  1. Java基础-多线程学习目录

    1.Java多线程并发编程一览笔录 2.什么时候使用CountDownLatch 3.Java并发学习系列-绪论

  2. LinkedBlockingQueue源码分析

    1. LinkedBlockingQueue源码分析(JDK8) 2. LinkedBlockingQueue源码分析 啦啦啦

  3. java面试复习题四

    一.redis最大缓存和回收策略 二.常用的数据库Druid线程池的参数设置 三.Spring的几大特性和应用 参考 Spring的核心特性就是IOC和AOP,IOC(Inversion of Con ...

  4. Golang 笔记 2 函数、结构体、接口、指针

    一.函数 Go中函数是一等(first-class)类型.我们可以把函数当作值来传递和使用.Go中的函数可以返回多个结果.  函数类型字面量由关键字func.由圆括号包裹声明列表.空格以及可以由圆括号 ...

  5. Linux系统(本例以Ubuntu18.04为例)安装GCC编译器

    Linux(本例以Ubuntu18.04为例)安装GCC编译器 一.安装 安装命令:sudo apt-get  build-dep  gcc 遇到 您希望继续执行吗? [Y/n] y 直接输入y回车即 ...

  6. [原]Docker-issue(1) image name 显示为 <none>

    问题:今天发现重新上传新的image的时候覆盖了原来的镜像后,REPOSITORY 就变为了 <none> ,如下图 解决办法: 使用tag重新命名image 问题解决:

  7. Linux服务器性能分析与调优

    一 linux服务器性能查看 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc ...

  8. vue重要项目的参考

    https://github.com/PanJiaChen/vue-element-admin vue项目参考  重点 https://github.com/opendigg/awesome-gith ...

  9. linux操作2

    第2天 linux操作系统的目录结构 bin   #可执行程序的安装目录,命令boot #系统启动引导目录dev #设备目录,deviceetc #软件配置文件目录home #用户的家目录lib #系 ...

  10. 利用docker搭建ubuntu+nginx+PHP容器

      环境:操作系统(Ubuntu  16.04 64位); php7.1;  nginx/1.14.0   基础环境准备: 整体思路:docker pull一个ubuntu镜像,然后在容器中安装ngi ...