题意:

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

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

思路:

枚举全黑和全白的情况。

对于每一个需要移动的棋子,它移动到的位置一定是从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. 通用 正则表达式 C# (.NET)Regex 总结

    [参考]C#正则表达式Regex类的用法    语法: 1. new System.Text.RegularExpressions.Regex("\\$\\d{1,2}\\}"). ...

  2. 同步调用异步方法how-would-i-run-an-async-taskt-method-synchronously

    同步调用异步方法帮助类: public static class AsyncHelpers { /// <summary> /// Execute's an async Task<T ...

  3. oneNote2016代码高亮插件

    下载地址:https://github.com/elvirbrk/NoteHighlight2016/releases 安装插件前必须安装oneNote笔记. NoteHighlight插件有32位和 ...

  4. layui 笔记

    弹出层 点击事件 <!DOCTYPE html> <html> <head> <title></title> {load href=&quo ...

  5. Unity游戏推送技术

    https://www.cnblogs.com/wuzhang/p/wuzhang20150401.html https://www.cnblogs.com/yangwujun/p/5789969.h ...

  6. Springboot中如何在Utils类中使用@Autowired注入bean

    Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...

  7. git 同步远程已删除的分支和删除本地多余的分支

    使用git branch -a可以查看本地分支和远程分支情况 但远程分支(红色部分)删除后,发现本地并没有同步过来. 一. 同步本地的远程分支 查看本地分支和追踪情况: git remote show ...

  8. [转]解决Error: That port is already in use.

    ubuntu系统下,运行一个django项目,即输入python manage.py runserver后,可能出现 Error: That port is already in use.的错误. 即 ...

  9. Inside The C++ Object Model(三)

    ============================================================================3-0. 类所占的内存大小受到三个因素的影响:( ...

  10. A - The Water Problem

    In Land waterless, water is a very limited resource. People always fight for the biggest source of w ...