codeforces 931 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 ofy1, 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.

长时间不做题,加之过了一个寒假,简单题都做的毫无头绪,唉~~~

思路也懒得写,直接借鉴一下别人~~~

题意:K的答案是一组数,其中的最大值与最小值的差不超过2。A想抄作业,但遵循下列规则

  • A的平均值要和K的相同
  • A的最小值不小于K的,A的最大值不大于K的

  • A的答案中出现和K相同的数的总数应该尽可能少(可以看成A和K中相同的数两两配对,这种配对的个数尽可能少)。

思路:最大值与最小值之差不大于2 ,说明K的答案是有相邻的三个数组成的;A的最小值不小于K的,A的最大值不大于K的 ,说明A的答案也是由那三个数组成的。要保证K的平均值不变,数的种类也不变,而且组成K的数字是连续的,所以可以对K进行的变化,不过就是两种:把两个中间值转化为一大一小,或者把一大一小转化为两个中间值。

这是两个单调的过程,所以我们可以直接比较两个极端情况:尽可能把全部中间值转化为一大一小,或尽可能把所有一大一小的配对转化为两个中间值,哪一个是最优的结果。

那么算法就出来了,假设K由x, x+1, x+2组成,分别有a, b, c个。那么A的答案有三种可能的方案:

 xx+1x+2节省
方案1a+b/20c+b/2b
方案2a-cb+2*c02*c
方案30b+2*ac-a2*a

那么现在无非就是比较一下b与min(2*c,2*a)中大的,就是可以节约多少,n-max(b,min(2*c,2*a))就是最少相同的数目。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int a[100010],ans[3];
int main()
{
    int n,mn;
    while(~scanf("%d",&n))
    {
        mn=INF;
        memset(ans,0,sizeof(ans));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            mn=min(a[i],mn);
        }
        for(int i=0; i<n; i++)
        {
            a[i]-=mn;
            ans[a[i]]++;
        }
        sort(a,a+n);
        if(a[n-1]<2)
        {
            printf("%d\n",n);
            for(int i=0; i<n; i++)
                printf("%d ",a[i]+mn);
        }
        else
        {
            int temp=min(ans[0],ans[2]);
            int temp2=ans[1];
            if(temp2>temp*2)
            {
                ans[0]=ans[0]+temp2/2;
                ans[1]=ans[1]-temp2/2*2;
                ans[2]=ans[2]+temp2/2;
            }
            else
            {
                ans[0]=ans[0]-temp;
                ans[1]=ans[1]+temp*2;
                ans[2]=ans[2]-temp;
            }
            printf("%d\n",n-max(2*temp,temp2/2*2));
            for(int i=0; i<3; i++)
                for(int j=0; j<ans[i]; j++)
                    printf("%d ",i+mn);

        }
        printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值