codeforces 985A Chess Placing
题意:
移动最少的步数,使得所有的棋子在同一颜色的格子中。
每次一个棋子只能向左或者向右移动一步,不能移到有棋子的格子中。
思路:
枚举全黑和全白的情况。
对于每一个需要移动的棋子,它移动到的位置一定是从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的更多相关文章
- codeforce 985A Chess Placing(暴力)
Chess Placing time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- codeforces 893A Chess For Three 模拟
893A Chess For Three 思路: 直接模拟即可,第一盘永远是A与B开始 代码: #include <bits/stdc++.h> using namespace std; ...
- A. Chess Placing
链接 [https://codeforces.com/contest/985/problem/A] 题意 给你一个偶数n,输入n/2个数,代表棋子的位置,有一个1*n的棋盘是黑白相间的 问你使得所有棋 ...
- Codeforces 38B - Chess
38B - Chess 思路:懂点象棋的规则就可以,看看哪些点可以放马. 代码: #include<bits/stdc++.h> using namespace std; #define ...
- codeforces 845A Chess Tourney
参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...
- CF985A Chess Placing【思维】
[链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ...
- Codeforces 845A. Chess Tourney 思路:简单逻辑题
题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力. 实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢. 叫你把这2*n个选手分成2个有n个选手的队伍. ...
- Educational Codeforces Round 44 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
随机推荐
- win10下VS2017配置GSL库
GSL库:GNU Scientific Library 1. 下载:下载Complete package, except sources和Sources两个exe文件 2. 安装:将两个exe安装 ...
- Qt编写自定义控件3-速度仪表盘
前言 速度仪表盘,写作之初的本意是用来展示当前测试的网速用的,三色圆环+数码管显示当前速度,Qt自带了数码管控件QLCDNumber,直接集成即可,同时还带有动画功能,其实也可以用在汽车+工业领域等, ...
- Apache kylin进阶——元数据篇
一.Apache kylin元数据的存储 Apache kylin的元数据包括 立方体描述(cube description),立方体实例(cube instances)项目(project).作业( ...
- B - Tree Recovery
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- mysql 计算两点经纬度之间的直线距离(具体sql语句)
文章转载地址 http://blog.sina.com.cn/s/blog_7bbfd5fd01017d1e.html 新增sql语句具体实现 计算距离(单位 m)并排序 longitude 经度 l ...
- tensorflow学习笔记————分类MNIST数据集
在使用tensorflow分类MNIST数据集中,最容易遇到的问题是下载MNIST样本的问题. 一般是通过使用tensorflow内置的函数进行下载和加载, from tensorflow.examp ...
- 【C++ 实验5 类和对象】
1. #include <iostream> #include <vector> #include <string> using namespace std; // ...
- CF3A Shortest path of the king
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- 一、HTML基础学习
1.基本格式<html> <head><title></title></head> <body></body>< ...
- ubuntu安装python3.6
环境: ubuntu18.04 64位,python3.6.5 安装过程 1.打开终端 首先创建安装目录, sudo mkdir /usr/local/python3 2.然后下载安装包,解压,并且进 ...