Codeforces Round #468 Div. 2 C. Laboratory Work

Codeforces Round #468 Div. 2 C. Laboratory Work

题目

http://codeforces.com/problemset/problem/931/C

题意

小明抄小红的作业,想尽可能的不被老师发现。意思就是小明和小红的作业数据必须尽少的相似,但是他们的平均数必须相同。
小红的作业,最大值和最小值之差不超过2,小明只能再这个[min,max]范围内抄数据。
结果输出小明和小红相同数据的个数,然后再输出小明的数据。

题解

要平均数相同,实际上就是要总和相同。小红的数据一共就三个数a,b,c。我们统计a,b,c三个数的个数。2个b可以换成1个a和1个c,同时a和c也可以换成2个b。我们判断这两种情况那个情况重合的数据最少就输出那种情况。
要注意判断a没有和c没有的情况。

AC代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
#define ll long long
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[100005];
int main()
{
	int n;
	scanf("%d",&n);
	int temp[3];
	int num[3];
	mem(temp,0);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	temp[0] = 1;
	num[0] = a[0],num[1] = a[0] + 1,num[2] = a[0] + 2;
	for(int i=1;i<n;i++)
	{
		if(a[i] - num[0] == 0)
			temp[0]++;
		else if(a[i] - num[0] == 1)
			temp[1]++;
		else
			temp[2]++;
	}
	if(temp[0] == 0 || temp[2] == 0)
	{
		printf("%d\n",n);
		for(int i=0;i<n;i++)
		{
			if(i)
				printf(" %d",a[i]);
			else
				printf("%d",a[i]);
		}
	}
	else if(temp[0] + temp[2] + temp[1]%2 <= temp[1] + abs(temp[0] - temp[2])) 
	{
		printf("%d\n",temp[0] + temp[2] + temp[1]%2);
		temp[0] += temp[1]/2;
		temp[2] += temp[1]/2;
		temp[1] = temp[1]%2;
		for(int i=0,j=0;i<n;i++)
		{
			while(temp[j] == 0)
				j++;
			if(i)
				printf(" %d",num[j]);
			else
				printf("%d",num[j]);
			temp[j]--;
		}
		puts("");
	}
	else if(temp[0] + temp[2] + temp[1]%2 > temp[1] + abs(temp[0] - temp[2])) 
	{
		printf("%d\n",temp[1] + abs(temp[0] - temp[2]));
		int t = min(temp[0],temp[2]);
		temp[0] -= t;
		temp[2] -= t;
		temp[1] += 2*t;
		for(int i=0,j=0;i<n;i++)
		{
			while(temp[j] == 0)
				j++;
			if(i)
				printf(" %d",num[j]);
			else
				printf("%d",num[j]);
			temp[j]--;
		}
		puts("");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值