版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/100170399
You are given n chips on a number line. The i-th chip is placed at the integer coordinate
. 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:
with
or with
);
with
or with
).
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
should be equal after some sequence of moves).
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
,
,…,
(1 ≤
≤ 109), where
is the coordinate of the i-th chip.
Print one integer — the minimum total number of coins required to move all n chips to the same coordinate.
3
1 2 3
1
5
2 2 2 3 3
2
这道题目的大意说白了就是输出奇偶数中个数较少的那个。判断奇偶性的时候用n&1或者n%2都行,这俩个是等价的。
#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;
}