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;
}