【Codeforces】1213A - Chips Moving

Problem Description:

You are given n chips on a number line. The i-th chip is placed at the integer coordinate 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} with x_{i}-2 or with 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} with x_{i}-1 or with 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} 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_{2},…,x_{n} (1 ≤ x_{i} ≤ 109), where 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:

3
1 2 3

Sample Output1:

1

Sample Input2:

5
2 2 2 3 3

Sample Output2:

2

解题思路:

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

AC代码:

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢ctrl的cxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值