zoukankan      html  css  js  c++  java
  • Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round) 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.

    分析:

    如果 题目给出数组,极差小于2,那么无法进行调整。

    如果极差等于2, 那么有两种方案,如果最小的数为设为x ,第一种是把2个x+1分为x和x+2;第二种是把一对x和x+2化为 2个x+1

    选择两种方案的最优方案即可

    代码如下:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    const int MAXN=1e5+10;
    int a[MAXN];
    vector<int>V;
     int n,start,cnt1,cnt2,cnt3,res1,res2,num1,num2,num3;
    void print()
    {
        int flag=0;
       for(int i=1;i<=cnt1;i++)
       V.push_back(start);
        for(int i=1;i<=cnt2;i++)
         V.push_back(start+1);
         for(int i=1;i<=cnt3;i++)
          V.push_back(start+2);
          for(int i=0;i<V.size();i++)
          i==0?cout<<V[i]:cout<<" "<<V[i];
          cout<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin>>n;
        cnt1=cnt2=cnt3=0;
        for(int i=1;i<=n;i++)
          cin>>a[i];
        sort(a+1,a+n+1);
        if(a[n]-a[1]<2)
        {
            cout<<n<<endl;
            for(int i=1;i<=n;i++)
            i==1?cout<<a[i]:cout<<" "<<a[i];
            cout<<endl;
        }
        else
        {
            start=a[1];
           for(int i=1;i<=n;i++)
           {
             if(a[i]==start)
              cnt1++;
             else if(a[i]==start+1)
              cnt2++;
             else
              cnt3++;
           }
           res1=cnt2/2*2;
           res2=min(cnt1,cnt3)*2;
           int ans=n-max(res1,res2);
           cout<<ans<<endl;
          if(res1>res2)
          {
             cnt2%=2;
             cnt1+=res1/2;
             cnt3+=res1/2;
          }
          else
          {
             cnt2+=res2;
             cnt1-=res2/2;
             cnt3-=res2/2;
          }
          print();
        }
        return 0;
    }

                                                                                          

  • 相关阅读:
    mysql 百万级查询优化
    hibernate N+1
    sql 技巧
    redis做成windows服务
    jsonp 跨域
    maven+spring-data-jpa环境搭建
    通过浏览器地址进行 post get 请求
    spring-data-jpa 新增 修改 删除 查询 分页
    mybatis+springMVC
    java 基于 bootstrap_datagrid 分页
  • 原文地址:https://www.cnblogs.com/a249189046/p/8513519.html
Copyright © 2011-2022 走看看