Codeforces 931.C Laboratory Work

C. Laboratory Work
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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: x1, x2, ..., 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 y1, y2, ..., 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.

Examples
input
Copy
6
-1 1 1 0 0 -1
output
2
0 0 0 0 0 0
input
Copy
3
100 100 101
output
3
101 100 100
input
Copy
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.

题目大意:给你一个长度为n的序列,这个序列最大值与最小值的差不超过2. 你需要构造一个序列,使得这个序列的平均值与给定序列的平均值相同,最大值与最小值的差不超过2,并且相同元素的个数最少.

分析:先考虑如何使得平均值相同.  因为数列的极差不超过2嘛,那么就相当于数列中只会存在3种数:1,2,3.(第几大). 新序列中2的数量每增加2,就有一对1,3减少,因为增加和减少的是相同的,所以平均值不变,并且2≠1,3,又能保证相同的元素最少.  所以方法就是:看原序列有多少对(1,3),有多少对就添加多少对*2个2,剩下的和原序列保持相同就好了.

   上面说的只是一种情况:2的数量比(1,3)少.如果比(1,3)多的话,就把2都变成1,3就好了.

#include<iostream>
#include<algorithm>
#include<string>
#include <queue>
#include<cstring>
#include<cstdio>

using namespace std;

typedef long long ll;
const ll maxn=100010;
const ll inf=1e9+7;
ll n,a[maxn];
ll minv=inf;
ll sum[3];
int main()
{
    scanf("%I64d",&n);
    for(ll i=1; i<=n; i++)
    {
        scanf("%I64d",&a[i]);
        minv = min(minv,a[i]);
    }
    for(ll i=1; i<=n; i++)
        a[i]-=minv;
    for(ll i=1; i<=n; i++)sum[a[i]]++;
    sort(a+1,a+n+1);
    if(a[n]<2)
    {
        printf("%I64d\n",n);
        for(ll i=1; i<=n; i++)printf("%I64d ",a[i]+minv);
        return 0;
    }
    else
    {
        ll temp = min(sum[0],sum[2]);
        ll temp2=sum[1] >> 1;
        if(temp>temp2)
        {
            sum[1]+=(temp << 1);
            sum[0]-=temp;
            sum[2]-=temp;
        }
        else
        {
            temp=temp2;
            sum[1]-=(temp << 1);
            sum[0]+=temp;
            sum[2]+=temp;
        }
        printf("%I64d\n",n-(temp << 1));
        for(ll i=0; i<3; i++)
            for(ll j=1; j<=sum[i]; j++)printf("%I64d ",i+minv);
    }

    return 0;
}

 

posted @ 2018-03-05 14:24  zbtrs  阅读(384)  评论(0编辑  收藏  举报