【Educational Codeforces Round 3 C】【贪心 排序】Load Balancing 最小操作步数使得数字之差尽可能小

来源:互联网 发布:数组转化成json 编辑:程序博客网 时间:2024/06/05 10:07
C. Load Balancing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.

In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.

In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.

Write a program to find the minimum number of seconds needed to balance the load of servers.

Input

The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers.

The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.

Output

Print the minimum number of seconds required to balance the load.

Sample test(s)
input
21 6
output
2
input
710 11 10 11 10 11 11
output
0
input
51 2 3 4 5
output
3
Note

In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.

In the second example the load is already balanced.

A possible sequence of task movements for the third example is:

  1. move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5);
  2. then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4);
  3. then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3).

The above sequence is one of several possible ways to balance the load of servers in three seconds.


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=1e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;int n;LL a[N];int main(){while(~scanf("%d",&n)){LL sum=0;for(int i=1;i<=n;++i)scanf("%lld",&a[i]),sum+=a[i];LL ave=sum/n;int num=sum%n;sort(a+1,a+n+1);LL val=0;for(int i=n;i>n-num;--i)val+=abs(a[i]-(ave+1));for(int i=1;i<=n-num;++i)val+=abs(a[i]-ave);printf("%lld\n",val/2);}return 0;}/*【题意】给你n(1e5)个数a[],我们可以把任一个数拿出1,给与另外任意一个数。这样我们到最后,有一个最大的数ma和一个最小的数mb,我们希望使得ma-mb尽可能小。并且,在实现这个目标的前提下,使得操作的步数尽可能少。【类型】贪心 排序【分析】我们从讨论比较少的地方入手。很显然,ma-mb的值,只可能是两种——0或者1。0:这个时候,显然,操作步数就是∑(a[i]-ave)/21:这个时候,我们最终肯定有一些数(n-sum%n个)变成x,一些数(sum%n个)变成x+1.于是,我们选择让最小的那n-sum%n个数变成x,让最大的那sum%n个数变成x+1就好啦。计数还是总的步长差/2【时间复杂度&&优化】O(n)*/


0 0