Codeforces Round #468 C. Laboratory Work

C. Laboratory Work

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 

题意:构造题,给你n个数的数列A,让你再构造n个数的新数列A1,A1的平均值要等于A,同时A1里最大元素不超过A最大,最小不小于A最小,最后尽可能让A1与A相等的元素个数最少。输出A1与A相等的元素个数,同时输出A1。

思路:题中已经明确,数列中极差不超过2,可得规律:A数列中最多只有3种数字,最少1种。

排个序,a[n]-a[1]求极差:

1:如果是0,说明所有元素相等,此时无法替代,直接输出原数列。

2:如果是1,此时必然只有两种数字,平均数依然无法用别的数字替代,输出原数列。

3:如果是2,此时必然有三种数字,有两种更改方案,-1 -1 0 0 1 1 这种两端更改的求一遍。-1 0 0 0 0 1 这种两个相邻的0可以变成1 -1,更改相邻的也求一遍,取更改的最多的一个即可,改的多,即剩的少,与原数列相等的越少。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100010],a1[100010],n;
int main()
{
    while(~scanf("%lld",&n))
    {
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            a1[i]=a[i];   //多开一个数组,一会要比较两种方案输出
        }
        sort(a+1,a+n+1);
        sort(a1+1,a1+n+1);
        if(a[n]-a[1]==0||a[n]-a[1]==1)   //特判直接输出的
        {
            printf("%lld\n",n);
            for(ll i=1;i<=n;i++)printf("%lld ",a[i]);
            printf("\n");
            continue;
        }
        ll l=1,r=n,tmp,ans1,ans2;
        ans1=ans2=n;
        while(l<r)   //两端更改的
        {
            tmp=a[l]+a[r];
            if(tmp%2==0)
            {
                tmp/=2;
                if(tmp!=a[l])  
                {
                    a[l]=a[r]=tmp;
                    ans1-=2;
                }
            }
            l++;r--;
        }
        for(ll i=2;i<=n;i++)   //更改相邻的
        {
            if(a1[i]==a1[i-1])
            {
                if(a1[i]==(a1[1]+a1[n])/2)
                {
                    a1[i-1]=a1[1];
                    a1[i]=a1[n];
                    ans2-=2;
                }
            }
        }
        if(ans1<=ans2)   //比较输出最优的
        {
            printf("%lld\n",ans1);
            for(ll i=1;i<=n;i++)printf("%lld ",a[i]);
            printf("\n");
        }
        else
        {
            printf("%lld\n",ans2);
            for(ll i=1;i<=n;i++)printf("%lld ",a1[i]);
            printf("\n");
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值