CodeForces - 931C (思维题,最少相等数)

Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value n times, and then compute the average value to lower the error.

Kirill has already made his measurements, and has got the following integer values: x1x2, ..., xn. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2.

Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values y1y2, ..., yn in her work, that the following conditions are met:

  • the average value of x1, x2, ..., xn is equal to the average value of y1, y2, ..., yn;
  • all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;
  • the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work.

Help Anya to write such a set of measurements that the conditions above are met.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the numeber of measurements made by Kirill.

The second line contains a sequence of integers x1, x2, ..., xn ( - 100 000 ≤ xi ≤ 100 000) — the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values x1, x2, ..., xn does not exceed 2.

Output

In the first line print the minimum possible number of equal measurements.

In the second line print n integers y1, y2, ..., yn — the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.

If there are multiple answers, print any of them.

Example
Input
6
-1 1 1 0 0 -1
Output
2
0 0 0 0 0 0 
Input
3
100 100 101
Output
3
101 100 100 
Input
7
-10 -9 -10 -8 -10 -9 -9
Output
5
-10 -10 -9 -9 -9 -9 -9 
Note

In the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements.

In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal.

In the third example the number of equal measurements is 5.


题意:  Anya想偷(借)一下Kirill的物理实验数据(跟我一样,我也喜欢这样做), Kirill的数据最大值和最小值差小于等于2.


因为老师会检查两个人的实验数据,看看两个相同的数据有多少?


Anya想相同数据最少,然后自己的最大值不大于他的数据的最大值,自己的最小值不小于他数据的最小值


如果存在多组答案的话,输出其中一个就行了。

思路:因为最大值和最小值的差值最大为2,当最大值Ma和最小值Mi 的差距为0或1时直接输出就行,当为2时,刚开始我就想出了一种情况,让一个最小值和一个最大值变成两个中间值,但是 wrong,一定要考虑问题周全,也可以用两个中间值变成一个最大值和一个最小值。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100100
#define M 0x3f3f3f3f
int a[Max];
int Ma,Mi,n;
void ppp(int t1,int t2,int t3)
{
	int i;
	int sum = 0;
	int k = Mi + 1;
	int f = 1;
	if(t3==0)
	{
		t2 = t2 - 1;
		f=0;
	} 
	else t3 = t3 - 1;
	for(i = 0; i< t1;i++)
		printf("%d ",Mi);
	for(i = 0;i< t2; i++)
		printf("%d " ,Mi + 1);
	for(i = 0;i < t3; i++)
		printf("%d ",Ma);
	if(f==0) printf("%d\n",Mi + 1);
	else printf("%d\n",Ma);
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		Mi =  M,Ma = -M;
		for(i = 0; i < n; i ++)
		{
			scanf("%d",&a[i]);
			if(a[i]<Mi)
				Mi = a[i];
			if(a[i] > Ma)
				Ma = a[i];
		}
		if(Ma - Mi < 2)
		{
			printf("%d\n",n);
			for(i = n-1;i>=0;i--)
			{
				if(i == 0) printf("%d\n",a[i]);
				else printf("%d ",a[i]);
			}
		}
		else 
		{
			int sum1 = 0, sum2 = 0, sum3 = 0;
			for(i = 0;i < n;i++)
			{
				if(a[i] == Mi)
					sum1 ++;
				if(a[i] == Ma)
					sum3 ++; 
			}
			sum2 = n - sum1 - sum3;
			int k = sum2 - sum2 % 2;
			if(k > 2*min(sum1,sum3))
			{
				printf("%d\n",n-k);
				ppp(sum1+k/2,sum2%2,sum3+k/2);
			}
			else
			{
				int tt = n - 2*min(sum1,sum3);
				printf("%d\n",tt);
				if(sum1 >= sum3)
					ppp(sum1 - sum3,sum2+2*sum3,0);
				else 
					ppp(0,sum2+2*sum1,sum3 - sum1);
			}
			
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值