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必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
随机推荐
- react文档demo实现输入展示搜索结果列表
文档页面地址:https://doc.react-china.org/docs/thinking-in-react.html 该文档只给了具体实现思路,下面是我实现的代码. 初学react,如果有写的 ...
- java相对路径、绝对路径及类路径
import java.io.File; import java.net.URL; /** * java相对路径.绝对路径及类路径的测试 */ public class Test { /** * 测试 ...
- 通过User-agent进行SQL注入
声明:本文由Bypass整理并翻译,仅用于安全研究和学习之用. 文章来源:https://hackerone.com/reports/297478 我发现了一个SQL注入漏洞 /dashboard/d ...
- GPRS模块在Linux平台上ppp拨号上网总结与心得
linux平台的ppp拨号上网,(注明:这里只谈命令行拨号,用linux就要习惯和熟练使用命令行) 在网上常见的有三种方式:1.使用智能的ppp拨号软件wvdial:参考案例:本博客的<使用wv ...
- 【thinkphp5.1】 endroid/qrcode 二维码生成
composer 链接: https://packagist.org/packages/endroid/qrcode 注意:PHP版本 要求 7.1+ 1. 使用 composer 安装 endroi ...
- 【Py-Github】根据条件筛选Github repo的例子
条件: language:python commits:>100 contributors:>2 stars:>5 fork:0 实现: from github import Git ...
- PHP异步请求之fsockopen()方法详解
正常情况下,PHP执行的都是同步请求,代码自上而下依次执行,但有些场景如发送邮件.执行耗时任务等操作时就不适用于同步请求,只能使用异步处理请求. 场景要求: 客户端调用服务器a.php接口,需要执行一 ...
- geopandas overlay 函数报错问题解决方案
前言 这篇文章依旧是基于上一篇文章(使用Python实现子区域数据分类统计)而写,此文章中介绍了使用 geopandas 的 overlay 函数对两个 GeoDataFrame 对象取相交或相异的部 ...
- nw.js---创建一个hello word的方法
一.如果用nw.js 来开发桌面应用 首先到Nw.js中文网下载软件: https://nwjs.org.cn/download.html 下载下来进行解压就可以了,绿色的免安装的,整个目录结果是这样 ...
- gitlab-ci + k8s 之docker (三)
docker 在本系列(一)中(https://www.cnblogs.com/huandada/p/9965771.html)的runner_tomcat.sh脚本有涉及到镜像的推送,本文主要记录整 ...