Codeforces Round #468 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.

以后要不要考虑倒着写题?
解析:mx-mi<=1直接输出原数组就好了,然后有两种策略,将中间两个分成旁边两个或者将旁边两个合成中间两个,暴力就好了.

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=1e5+100;
int arr[N];
int main()
{
    ios::sync_with_stdio(false);
    int n,sum=0,mx=-inf,mi=inf;
    cin>>n;
    map<int,int>mp;
    rep(i,0,n)
    {
        cin>>arr[i],sum+=arr[i];
        mp[arr[i]]++;
        mi=min(mi,arr[i]);mx=max(mx,arr[i]);
    }
    if(mx-mi<=1)
    {
        cout<<n<<endl;
        rep(i,0,n)
        cout<<arr[i]<<' ';
        cout<<endl;
    }
    else
    {
        int a=mp[mi],b=mp[mx-1],c=mp[mx];
        int d=mp[mi],e=mp[mx-1],f=mp[mx];
        int ans=n+1;
        int a1,b1,c1;
        while(b>1)
        {
            b-=2;
            a++,c++;
            if(ans>=d+f+b)
            {
                ans=d+f+b;
                a1=a,b1=b,c1=c;
            }
        }
        a=d,b=e,c=f;
        while(a>0&&c>0)
        {
            a--,c--;
            b+=2;
            if(ans>=a+c+e)
            {
                ans=min(ans,a+c+e);
                a1=a,b1=b,c1=c;
            }

        }
        cout<<ans<<endl;
        rep(i,0,a1)
            cout<<mi<<' ';
        rep(i,0,b1)
        cout<<mi+1<<' ';
        rep(i,0,c1)
        cout<<mi+2<<' ';
    }
    return 0;
}

转载于:https://www.cnblogs.com/ffgcc/p/10546488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值