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: \(x_1, x_2, ..., x_n\). 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 \(y_1, y_2, ..., y_n\) in her work, that the following conditions are met:
the average value of \(x_1, x_2, ..., x_n\) is equal to the average value of \(y_1, y_2, ..., y_n\);
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 \(x_1, x_2, ..., x_n ( - 100 000 ≤ x_i ≤ 100 000)\) — the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values \(x_1, x_2, ..., x_n\) does not exceed \(2\).
Output
In the first line print the minimum possible number of equal measurements.
In the second line print n integers \(y_1, y_2, ..., y_n\) — 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
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\).
题意
给出一个数组\(a\),要求构造一个新的数组\(b\),满足:
- \(b\)数组的平均值和\(a\)数组的平均值相等,
- \(b\)数组的最大值不大于\(a\)数组的最大值,最小值不小于\(a\)数组的最小值
- 要求\(a,b\)数组不同的元素尽可能多
思路
因为题目中给出了:数组中最大值和最小值的差值不超过\(2\),可以分成两种情况:
- 当\(a\)数组中的最大值和最小值差值小于\(2\)的时候,无法构造出一个和\(a\)不同的满足条件的\(b\)数组,相同元素个数为\(n\)
- 当最大值和最小值差值等于\(2\)时,可以将\(a\)数组转换成只有\(0,1,2\)的一个新数组,然后有两种方案进行构造:
- 方案一:将两个\(1\)变成一个\(0\)和一个\(2\)
- 方案二:将一个\(0\)和一个\(2\)变成两个\(1\)
当\(1\)较少的时候,选择方案二,当\(1\)比\(2\)和\(0\)的个数多的时候,选择方案一
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int a[maxn];
int num[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
int minn=inf;
int maxx=-inf;
for(int i=0;i<n;i++)
{
cin>>a[i];
minn=min(a[i],minn);
maxx=max(maxx,a[i]);
}
if(maxx-minn<=1)
{
cout<<n<<endl;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{
for(int i=0;i<n;i++)
a[i]-=minn,num[a[i]]++;
// 两个1变成一个0和一个2
// 一个0和一个2变成两个1
int res=min(num[0],num[2]);
int res1=num[1]/2;
// 如果1比较少,选择方案二
if(res>res1)
{
num[1]+=res*2;
num[0]-=res;
num[2]-=res;
}
// 如果1比较多,选择方案一
else
{
res=res1;
num[1]-=res*2;
num[0]+=res;
num[2]+=res;
}
cout<<n-res*2<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<num[i];j++)
cout<<minn+i<<" ";
cout<<endl;
}
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
#endif
return 0;
}
Codeforces 931C:Laboratory Work(构造)的更多相关文章
- CodeForces 931C Laboratory Work 水题,构造
*这种题好像不用写题解... 题意: 一个人要改动别人的实验记录,实验记录记录是一个集合 实验记录本身满足:$max(X)-min(X)<=2$ 改动结果要求: 1.新的集合平均值和之前的一样 ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- codeforces 22C System Administrator(构造水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud System Administrator Bob got a job as a s ...
- Codeforces 353D Queue(构造法)
[题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- [ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造
题意是让你用1到n的数构造24 看完题解感觉被样例骗了…… 很明显 n<4肯定不行 然后构造出来4 5的组成24的式子 把大于4(偶数)或者5(奇数)的数构造成i-(i-1)=1 之后就是无尽的 ...
随机推荐
- 日常Java 2021/10/27
java HashMap HashMap是一个散列表,它存储的内客是键值对(key-value)映射.HashMap实现了Map.接口,根据键的HashCode值存储数据,具有很快的访问速度,最多允许 ...
- 学习java 7.12
学习内容: File是文件和目录路径名的抽象表示,File封装的不是一个真正存在的文件,仅仅是一个路径名 File类的方法 绝对目录和相对目录的区别 字节流 使用字节输出流写数据的步骤 : 创建字节输 ...
- day06 视图层
day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...
- Git(一)【基本使用,集成IDEA,GitHub】
目录 一.本地库操作 ①基本操作 1.初始化本地库 2.设置用户签名|用户名|邮箱 3.查看本地库状态 4.添加暂存区 5.提交到本地库 6.查看文件modify详情 ②历史版本以及回退 1.查看历史 ...
- 几种常用JavaScript设计模式es6
设计模式分类(23种设计模式) 创建型 单例模式 原型模式 工厂模式 抽象工厂模式 建造者模式 结构型 适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式 行为型 观察者模式 迭 ...
- CSS系列,三栏布局的四种方法
三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...
- D3-更改x轴的标签
记录,上代码
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
遇到这个一场折腾了1个小时, 这是系统在解析XML的时候出错, 最后费了好大的劲才发现 XML文件中,<View> 写成小写的 <view> 了. 崩溃啊.......... ...
- OC-代理,字符串
总结 编号 标题 内容 一 protocol protocol 基本概念/语法格式/protocol和继承区别/使用注意/基协议/@required和@optional关键字/类型限制 二 代理设计模 ...
- 【Spring Framework】Spring入门教程(三)使用注解配置
本文主要介绍四个方面: (1) 注解版本IOC和DI (2) Spring纯注解 (3) Spring测试 (4) SpringJDBC - Spring对数据库的操作 使用注解配置Spring入门 ...