链接

[https://codeforces.com/contest/985/problem/A]

题意

给你一个偶数n,输入n/2个数,代表棋子的位置,有一个1*n的棋盘是黑白相间的

问你使得所有棋子在同一种颜色所需移动的最小步数

分析

先对所有棋子的位置排序

贪心枚举两种颜色都算在比较

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int a[110];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
int n;
while(cin>>n){
for(int i=1;i<=n/2;i++)
cin>>a[i];
sort(a+1,a+n/2+1);
int sum1=0,sum2=0;
int o=n-1,k=n;
for(int i=n/2;i>0;i--){
if(a[i]!=k) sum2+=abs(k-a[i]);
k-=2;
}
for(int i=n/2;i>0;i--)
{
if(a[i]!=o) sum1+=abs(o-a[i]);
o-=2;
}
cout<<min(sum1,sum2)<<endl;
}
return 0;
}

A. 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 985A Chess Placing

    题意: 移动最少的步数,使得所有的棋子在同一颜色的格子中. 每次一个棋子只能向左或者向右移动一步,不能移到有棋子的格子中. 思路: 枚举全黑和全白的情况. 对于每一个需要移动的棋子,它移动到的位置一定 ...

  4. CF985A Chess Placing【思维】

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

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

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

  6. hdu4405 Aeroplane chess

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 5742 Chess SG函数博弈

    Chess Problem Description   Alice and Bob are playing a special chess game on an n × 20 chessboard. ...

  8. POJ2425 A Chess Game[博弈论 SG函数]

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3917   Accepted: 1596 Desc ...

  9. HDU 4832 Chess (DP)

    Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. linux shell 指令 诸如-d, -f, -e之类的判断表达式简介

    一.文件比较运算符 1. e filename 如果 filename存在,则为真 如: [ -e /var/log/syslog ] 2. -d filename 如果 filename为目录,则为 ...

  2. C++借助curses库实现俄罗斯方块

    主要要实现如下几个功能:方块的移动控制.方块变形.判定方块是否接触边界和进行方块堆积.对方块进行消除. 1.方块的移动控制上下左右四个方向上-->变形,下-->加速下落,左-->向左 ...

  3. linux c 语言之--fseek(),fseeko(),fseeko64()讲解 (转载)

    转载:http://blog.csdn.net/lemoncyb/article/details/16841317 fseek() 函数讲解: 函数定义: int fseek(FILE *stream ...

  4. LeetCode算法题-Missing Number(Java实现-四种解法)

    这是悦乐书的第200次更新,第209篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第65题(顺位题号是268).给定一个包含n个不同数字的数组,取自0,1,2,...,n ...

  5. February 13th, 2018 Week 7th Tuesday

    You are your greatest asset. 你就是你自己最大的资本. For most of us, there are few things that we can count on ...

  6. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

  7. 《Java大学教程》—第18章 高级图形编程

    自测题:1.    在图形应用程序中为用户提供选择的多种方式:P433下拉菜单(pull-down menu).弹出式菜单(pop-up menu).对话框窗口(dialogue window).单选 ...

  8. 《Java大学教程》--第3章 迭代

    迭代(iteration).重复(repetition):三种循环* for: 重复执行固定次数* while: 重复执行不固定次数* do...while: 比while至少多一次 1.答:P47迭 ...

  9. lua 编译安装

    官网http://www.lua.org/download.html Building Lua is implemented in pure ANSI C and compiles unmodifie ...

  10. maven中可以直接引用的java系统属性和环境变量属性

    一.查看命令: 1 mvn help :system 二.引用 在pom文件中通过 ${变量名}来引用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...