第一发。。。codeforces 609 C Load Balancing 贪心

时间:2022-07-08 16:50:05
/*
题意:给你一个序列经过最小变换,变换一次为一个数+1,一个数-1,使得最大值与最小值相差1;
思路:与最后得到的序列相差的sum/2;
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll a[];
int main()
{
ll x,y,z,i,t;
scanf("%I64d",&x);
ll sum=;
for(i=;i<x;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
ll yu=sum%x;
ll c=sum/x;
ll cha=;sort(a,a+x);
for(i=x-;i>=;i--)
{
if(yu)
cha+=abs(a[i]-c-),yu--;
else
cha+=abs(a[i]-c);
}
printf("%I64d\n",cha/);
return ;
}