CodeForces - 1213A Chips Moving (思维 数学)

CodeForces - 1213A Chips Moving

题目

You are given n chips on a number line. The i-th chip is placed at the integer coordinate xi. 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 xixi with xi−2 or with xi+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 xixi with xi−1 or with xi+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 nn chips to the same coordinate (i.e. all xi should be equal after some sequence of moves).


Input

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 x1,x2,…,xn (1≤xi≤109), where xi is the coordinate of the i-th chip.


Output

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


Sample Input

3
1 2 3

Sample Output

1

Sample Input

5
2 2 2 3 3

Sample Output

2

题目大意:
题意是给你一些坐标,要求是把所有坐标都变成一样的。条件是每次移动两个位置免费,每次移动一个位置需要花费1硬币,问最少花费的硬币数是多少?

题目分析:

因为奇数坐标要想移动到偶数坐标上需要花费1硬币(无论怎样都需要花费一硬币),因为2个2个移动都是免费的,最后总是要剩下一个。偶数坐标移动到奇数坐标上也是如此。可以先统计奇数个数,最后比较奇数个和偶数个大小即可。

AC代码

#include <stdio.h>
int main()
{
	long int n,i,ou=0,ji=0;
	scanf("%d",&n);
	long int a[n];
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	for(i=0;i<n;i++)
	{
		if(a[i]%2==0)
		ou++;
		else 
		ji++;
	}
	if(ou>ji)
	printf("%d",ji);
	else
	printf("%d",ou);
	
	
}

我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。

发布了50 篇原创文章 · 获赞 50 · 访问量 2755

猜你喜欢

转载自blog.csdn.net/weixin_45691686/article/details/104231316