前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces】1213A - Chips Moving

【Codeforces】1213A - Chips Moving

作者头像
喜欢ctrl的cxk
发布2019-11-08 15:48:19
3070
发布2019-11-08 15:48:19
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/100170399

Problem Description:

You are given n chips on a number line. The i-th chip is placed at the integer coordinate

x_{i}
x_{i}

. Some chips can have equal coordinates.

You can perform each of the two following types of moves any (possibly, zero) number of times on any chip:

  • Move the chip i by 2 to the left or 2 to the right for free (i.e. replace the current coordinate
x_{i}
x_{i}

with

x_{i}-2
x_{i}-2

or with

x_{i}+2
x_{i}+2

);

  • move the chip i by 1 to the left or 1 to the right and pay one coin for this move (i.e. replace the current coordinate
x_{i}
x_{i}

with

x_{i}-1
x_{i}-1

or with

x_{i}+1
x_{i}+1

).

Note that it's allowed to move chips to any integer coordinate, including negative and zero.

Your task is to find the minimum total number of coins required to move all n chips to the same coordinate (i.e. all

x_{i}
x_{i}

should be equal after some sequence of moves).

Input Specification:

The first line of the input contains one integer n (1 ≤ n ≤ 100) — the number of chips.

The second line of the input contains n integers

x_{1}
x_{1}

,

x_{2}
x_{2}

,…,

x_{n}
x_{n}

(1 ≤

x_{i}
x_{i}

≤ 109), where

x_{i}
x_{i}

is the coordinate of the i-th chip.

Output Specification:

Print one integer — the minimum total number of coins required to move all n chips to the same coordinate.

Sample Input1:

代码语言:javascript
复制
3
1 2 3

Sample Output1:

代码语言:javascript
复制
1

Sample Input2:

代码语言:javascript
复制
5
2 2 2 3 3

Sample Output2:

代码语言:javascript
复制
2

解题思路:

这道题目的大意说白了就是输出奇偶数中个数较少的那个。判断奇偶性的时候用n&1或者n%2都行,这俩个是等价的。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)

int main()
{
    ios::sync_with_stdio(false);
    int n, odd = 0, even = 0;
    cin >> n;
    Up(i,1,n)
    {
        int _;
        cin >> _;
        (_%2 ? odd++ : even++);  //判断奇偶性,并记录奇偶数的个数
    }
    cout << min(odd,even) << endl;
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input1:
  • Sample Output1:
  • Sample Input2:
  • Sample Output2:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档