CodeForces - 931C 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, ..., ynin 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,那么就很好构造了。

1、极值差小于2时,构造的数组等于原数组。

2、极值差等于2时。由于平均值不变,那么就有两种构造方案:(1)将最大最小值合并为中间值。(2)将中间值分解为最大最小值。这样会得出2个ans。我们只需要输出ans最小的那一组解就可以了。

如果用数组存三个值的个数的话需要加一个偏移量。


代码:

输出占了很多行。。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
using namespace std;
#define inf 1<<29
int n,a[100005];
int vis1[200005],vis2[200005],vis3[200005];
int main()
{
    while(~scanf("%d",&n))
    {
        int minn=inf,maxx=-inf;
        memset(vis1,0,sizeof vis1);
        memset(vis2,0,sizeof vis2);
        memset(vis3,0,sizeof vis3);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            a[i]+=100000;
            vis1[a[i]]++;
            minn=min(minn,a[i]);
            maxx=max(maxx,a[i]);
        }
        if(maxx-minn<2)
        {
            printf("%d\n",n);
            int flag=0;
            for(int i=0;i<n;i++)
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",a[i]-100000);
            }
            printf("\n");
            continue;
        }
        memcpy(vis2,vis1,sizeof vis1);
        memcpy(vis3,vis1,sizeof vis1);
        int k=min(vis1[minn],vis1[maxx]);
        vis2[minn+1]+=k*2;
        vis2[minn]-=k;
        vis2[maxx]-=k;
        k=vis3[minn+1];
        if(k%2) k--;
        vis3[minn]+=k/2;
        vis3[maxx]+=k/2;
        vis3[minn+1]-=k;
        int ans1=vis2[minn]+vis1[minn+1]+vis2[maxx];
        int ans2=vis1[minn]+vis3[minn+1]+vis1[maxx];
        if(ans1<ans2)
        {
            printf("%d\n",ans1);
            int flag=0;
            while(vis2[minn])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn-100000);
                vis2[minn]--;
            }
            while(vis2[minn+1])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn+1-100000);
                vis2[minn+1]--;
            }
            while(vis2[minn+2])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn+2-100000);
                vis2[minn+2]--;
            }
        }
        else
        {
            printf("%d\n",ans2);
            int flag=0;
            while(vis3[minn])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn-100000);
                vis3[minn]--;
            }
            while(vis3[minn+1])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn+1-100000);
                vis3[minn+1]--;
            }
            while(vis3[minn+2])
            {
                if(flag) printf(" ");
                flag=1;
                printf("%d",minn+2-100000);
                vis3[minn+2]--;
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值