CodeForces - 931C (思维题,最少相等数)

Codeforces Round 931 (Div. 2)ABC 题面:给定由个元素组成的数组∣ai​−aj​∣∣aj​−ak​∣∣ak​−al​∣∣al​−ai​∣其中,是数组的四个索引,其中1≤ijkl≤n。这里的 || 表示绝对值。思路:因为n的数据范围是>=4,所以直接升序排序然后进行操作就行了。以|最大-最小| + |最小-次大| + |次大 -次小| + |次小-最大|forint;=;--longlong。 阅读详情

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.

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.


题意:  Anya想偷(借)一下Kirill的物理实验数据(跟我一样,我也喜欢这样做), Kirill的数据最大值和最小值差小于等于2.


因为老师会检查两个人的实验数据,看看两个相同的数据有多少?


Anya想相同数据最少,然后自己的最大值不大于他的数据的最大值,自己的最小值不小于他数据的最小值


如果存在多组答案的话,输出其中一个就行了。

思路:因为最大值和最小值的差值最大为2,当最大值Ma和最小值Mi 的差距为0或1时直接输出就行,当为2时,刚开始我就想出了一种情况,让一个最小值和一个最大值变成两个中间值,但是 wrong,一定要考虑问题周全,也可以用两个中间值变成一个最大值和一个最小值。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100100
#define M 0x3f3f3f3f
int a[Max];
int Ma,Mi,n;
void ppp(int t1,int t2,int t3)
{
	int i;
	int sum = 0;
	int k = Mi + 1;
	int f = 1;
	if(t3==0)
	{
		t2 = t2 - 1;
		f=0;
	} 
	else t3 = t3 - 1;
	for(i = 0; i< t1;i++)
		printf("%d ",Mi);
	for(i = 0;i< t2; i++)
		printf("%d " ,Mi + 1);
	for(i = 0;i < t3; i++)
		printf("%d ",Ma);
	if(f==0) printf("%d\n",Mi + 1);
	else printf("%d\n",Ma);
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		Mi =  M,Ma = -M;
		for(i = 0; i < n; i ++)
		{
			scanf("%d",&a[i]);
			if(a[i]<Mi)
				Mi = a[i];
			if(a[i] > Ma)
				Ma = a[i];
		}
		if(Ma - Mi < 2)
		{
			printf("%d\n",n);
			for(i = n-1;i>=0;i--)
			{
				if(i == 0) printf("%d\n",a[i]);
				else printf("%d ",a[i]);
			}
		}
		else 
		{
			int sum1 = 0, sum2 = 0, sum3 = 0;
			for(i = 0;i < n;i++)
			{
				if(a[i] == Mi)
					sum1 ++;
				if(a[i] == Ma)
					sum3 ++; 
			}
			sum2 = n - sum1 - sum3;
			int k = sum2 - sum2 % 2;
			if(k > 2*min(sum1,sum3))
			{
				printf("%d\n",n-k);
				ppp(sum1+k/2,sum2%2,sum3+k/2);
			}
			else
			{
				int tt = n - 2*min(sum1,sum3);
				printf("%d\n",tt);
				if(sum1 >= sum3)
					ppp(sum1 - sum3,sum2+2*sum3,0);
				else 
					ppp(0,sum2+2*sum1,sum3 - sum1);
			}
			
		}
	}
	return 0;
}


codeforces 构造题选做(难度1200) cf构造题 阅读详情

相关推荐

codeforces题目合集(持续更新中)

CF364DCF364DCF364D 随机化算法 CF438DCF438DCF438D 线段树 CF280CCF280CCF280C 期望dp CF1041CCF1041CCF1041C 双端队列 CF1045ACF1045ACF1045A 线段树优化建图+最大流 CF1045DCF1045DCF1045D 期望dp CF1051DCF1051DCF1051D 线性dp CF1051FCF1051...

苟为蒟蒻又何妨 8687

Codeforces思维题训练

Codeforces思维题训练

gyeolhada的博客 706

antiSMASH安装与使用

antiSMASH本地安装 简要: antiSMASH的本地安装需要安装很多的依赖包,本文章使用conda辅助安装 参考: 官方:https://docs.antismash.secondarymetabolites.org/install/ 文章:http://blog.sciencenet.cn/blog-3416913-1240614.html 依赖包下载 conda install -y diamond=0.8.36 conda install -y fasttree=2.1.9 conda ins

Curry_chenhu的博客 7222

codeforces 有意思的思维题 1 ~ 15

思维题1、给定数组,求满足i < j and ai * aj = i + j的数对数量2、第 i 步向前跳 i 步或后退 1 步3、 1、给定数组,求满足i < j and ai * aj = i + j的数对数量 codeforces 1541B. Pleasant Pairs 原题链接:https://codeforces.com/problemset/problem/1541/B 思路 因为3 <= i + j <= 2 * n - 1 那么 3 <= ai * aj

优快云Wudanna的博客 3518

VHDL按键消抖(状态机版本)

检测下降沿:设置一个和,分别代表之前的之前和之前的信号状态,若两个信号key_in_d2=1,key_in_d1=0,他俩的not key_in_d2 and key_in_d1为1,则说明下降沿产生,把它设置为标志位key_flag。延时计数:50MHz的板子每次计数需要20ns,而抖动时间大约为5-10ms。所以取7ms,作为延时时间。可以看见当检测到下降沿时,开始计数计数计满再次检测,若为低电平则为按下按键,否则为抖动。通过检测下降沿来临,延时计数再次检测,如果为低电平则为按键按下。

qq_69639216的博客 1083

STM32F401RET6寄存器手册阅读标记

对于时钟树配置,可参考STM32CubeMX中的时钟树配置,STM32CubeMX将基础配置图像化,较为简单。RCC_CR寄存器的HSIRDY位为只读位,当读取值为1时,表示HSI振荡器准备就绪,当HSION位被设置时,HSIRDY会自动设置为低电平。//问题标记:USART配置寄存器信息是如何与USART1/2/3建立联系的(为什么不是三个分别配置)查阅寄存器手册可知,需要配置时钟的模块有:USART1,IO port A。(2)TX配置方法(寄存器手册512页)(4)配置寄存器(寄存器手册548页)

m0_72615170的博客 1468

每天一道 CodeForces 构造/思维题 (day4)

构造题

小新的博客 564

openEuler 22.03 LTS SP4离线安装UKUI桌面全攻略(附依赖包下载)

本文提供openEuler 22.03 LTS SP4系统离线安装UKUI桌面环境的完整实战指南。针对无网络环境,详细解析了利用dnf工具下载依赖包、配置离线软件源、处理依赖冲突及安装后优化的全流程,并附带了关键依赖包资源,帮助运维人员高效完成图形化部署。

c7d8e9的博客 477

codeforces每日两道思维题(第一天)

an 的数组,使得和 (a1+a2+⋯+an) 能够被 k 整除,且数组 a 中的最大元素是可能的最小值。1)当n < k 时,我们让每个数组都取 k/n ,这样因为会出现下取整,整体答案是不够k的,那么再让一部分数字加上1,答案就是k/n+1。如果不能整数,那么就要扩大到距离最近的能够整除的数字,也就是令n = (n/k+1)*k;2)当n==k时,因为数字个数正好等于总和k,那直接让所有的ai = 1就是答案。若是本身n%k==0 那么直接让ai 都等于 n/k就是答案。那么此时的答案就是n/k。

m0_74036487的博客 204

复现论文《孤岛微电网环境下基于物理模型引导的深度强化学习VSG频率控制方法》

本文提出了一种结合物理模型与深度强化学习的孤岛微电网VSG频率控制方法。通过建立VSG小信号模型并设计可变系数控制器,将物理知识融入深度强化学习框架,解决了高维参数和机组耦合带来的控制难题。复现代码实现了三个核心组件:VSGSmallSignalModel建模VSG动态特性,VariableCoefficientController实现参数自适应调节,PhysicsGuidedDRL整合DQN算法与物理模型引导的奖励机制。该方法通过模型驱动与数据驱动的优势互补,提升了频率控制性能,其中物理模型既作为先验知识

huanghm88的专栏 127

【WRF模拟】如何查看 WPS 的输入静态地理数据(二进制格式)?

如何查看 WPS 的输入静态地理数据(二进制格式)?

WW、forever的博客 632

codeforces简单思维题合集

1030C 题意是问一个数列能否划分为几段使得每段的和相等(段数至少为2) 必然有一段是从1开始的 所以答案必然是n-1个前缀和中的一个 O(n)枚举,O(n)检验 #include &lt;bits/stdc++.h&gt; using namespace std; #define LL long long #define db double const int MAXN = ...

Karshilov的博客 3476

Android 13 内置xapk安装包的方法

安装xapk 内置xapk

安卓兼职framework和app工程师的博客 222

Codeforces Round 931 (Div. 2)

特殊的是,可能会出现用面额为15的硬币,余数为8,此时额外需要3枚硬币,但如果少用一枚15硬币,那么23的价值可以用10+10+3来替代,少用一枚硬币,类似这种情况就先减去当前枚举的面额,再枚举一次五种面额的硬币是否可能用更少的可能去替代。有五种不同面值的硬币{1, 3, 6, 10, 15},选取任意数量的硬币使得它们的总价值为n,最少选取多少硬币。分别找出n为1到15时需要的最少硬币,然后枚举用五种面额的硬币去计算时的需要多少个,剩下的需要补多少;怎么天天交互起来了,没白玩扫雷。描述稀碎,详见代码。

maisui12138的博客 942

OpenCV实例(八)车牌字符识别技术(三)汉字识别

相较于数字和英文字符的识别,汽车牌照中的汉字字符识别的难度更大,主要原因有以下4个方面:(1)字符笔画因切分误差导致非笔画或笔画流失。(2)汽车牌照被污染导致字符上出现污垢。(3)采集所得车辆图像分辨率低导致多笔画的汉字较难分辨。(4)车辆图像采集时所受光照影响的差异导致笔画较淡。综合汉字识别时的这些难点来看,很难被直接提取的是字符的局部特征。笔画作为最重要的特征而仅存在于汉字中,这由先验知识可知。一旦捺、横、竖、撇这些笔画特征被提取到,对于汉字字符识别的工作就完成了许多。

小幽余生不加糖 1650

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【codeforces】【速】931C

C. Laboratory Work 原题链接:931C. Laboratory Work 题意:给定一个最大值与最小值相差不超过2的数组,求一个平均值相同但数据相同个数最少的数组,并且需要输出他相同数据的个数,允许重复值 题解:如代码所示,利用桶,只有两种情况 a–>2b<–c — 变成 — 2b a<–2b–>c — 变成 — a c 考虑这两种情况中的最小值就好了...

Drmon.的博客 440

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Codeforces Round 971

Codeforces Round 971

青少年趣味编程 1514

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Codeforces Round 931 (Div. 2) A-C题解

贪心由题目中的两点差绝对值联想,我们可以想到两点在一条直线上的距离。题目要求我们的绝对值和最大,我们就取数组中最大的两个和最小的两个值,每个绝对值中都是一个最大的减一个最小的即可。而求数组中最大和最小的两个值,我们便可以对数组排序来获取到。

qq_28968763的博客 1268

Codeforces刷题记录

Codeforces刷题记录

〜( ̄△ ̄〜) yy 825

Codeforces 931 C. Laboratory Work

http://codeforces.com/problemset/problem/931/C 题意: 给定一个数列,要求构造一个等长的数列,使得数列的平均值等于给定数列,并且使得构造出的数列中与原数列相同的数字个数最小,输出最小相同数字个数与构造的数列。数列长度不超过100000,给定数列中最大的数字与最小的数字相差不超过2。 要求构造出的数列中最大值与最小值不能大于或小于原数列中的最...

weixin_34290000的博客 161

Codeforces Round 960

Codeforces Round 960

青少年趣味编程 705

Codeforces Round 931 (Div. 2)题解

思路:这里的操作是异或,所以我们从二进制的角度来考虑,显然只有两者二进制位不同的时候才需要改变,每一位上如果需要变,要么是0变1,要么是1变0,对于0变1,n实际上会变大,所以我们要找一个更高位1变0来和它同步变化,否则n^x>n,就不满足要求。所以我们可以先遍历它们二进制的每一位,然后标记一下每一位,如果是1变0,标记成1,如果是0变1标记成2,然后从小往大遍历,每次当一个标记为2的位需要改变的时候,我们就往前找到一个标记为1的位和它一起变,否则如果找不到就直接退出输出-1,如此便可解决。

m0_74120503的博客 892

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 meas...

光明~~~ 2万+

Codeforces Round 968

Codeforces Round 968

青少年趣味编程 706

Codeforces Round 1019

Codeforces Round 1019

青少年趣味编程 1749
上一篇: Codeforces Round #459 (Div. 2) D. MADMAX 博弈和记忆化搜索(同时照顾两个起点的记忆化搜索)
下一篇: CodeForces - 931D Peculiar apple-tree(模拟树的结构,一定要从树根到树枝搜索,因为是从树根发叉的,树根为一个点,树枝为多个点)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值