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: 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.

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.


题目大意:  给定一个序列,极差超过2,让你构建一个数目相同,平均数相同,极差不超过2,且和原数列相同数字最少的数列;要求构造出的数列中最大值与最小值不能大于或小于原数列中的最大值与最小值。

思路:  因为极差为2,那么原数列中就只有3种数,最大值,最小值,和中间值,且三种数之间都只差1,那么

1.若极差为0,那么原数列就是由相同的数组成的,所以新数列只能选择原数列;

2.若极差为1,同理,只能选择原数列;

3.若极差为2,我们用1,2,3分别来代表数列中的1->最小值 2->中间值 3->最大值,每增加一对(2,2),就会少一对(1,3)所以先比较(2,2)和(1,3)的数目如果(2,2)比(1,3)多那么就把(2,2)都变成(1,3)反过来也一样

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 100001

int a[N];

void read(int &x)
{
    x=0; int f=1; char c=getchar();
    while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
    x*=f;
}

int main()
{
    int n;
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    sort(a+1,a+n+1);
    if(a[n]-a[1]<=1) 
    {
        printf("%d\n",n);
        for(int i=1;i<=n;++i) printf("%d ",a[i]);
        return 0;
    }
    int s1=0,s2=0,s3=0;
    for(int i=1;i<=n;++i)
    {
        if(a[i]==a[1]) s1++;
        else if(a[i]==a[n]) s3++;
        else s2++;
    }
    int t=min(s1,s3),k=s2/2;
    int m=a[1];
    if(t>k)
    {
        for(int i=1;i<=t;++i) a[i]=m+1;
        for(int i=1;i<=t;++i) a[n-i+1]=m+1;        
        printf("%d\n",n-t*2);
        for(int i=1;i<=n;++i) printf("%d ",a[i]);
    }
    else
    {
        for(int i=1;i<=k;++i) a[s1+i*2-1]=m,a[s1+i*2]=m+2;
        printf("%d\n",n-k*2);
        for(int i=1;i<=n;++i) printf("%d ",a[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/acm513828825/article/details/79487426