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.

Examples
Input
2
1 6
Output
2
Input
7
10 11 10 11 10 11 11
Output
0
Input
5
1 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.

题意:

不描述了,好难描述啊。。。

思路:

直接求平均数,大于平均数的减,小于的加,最后得到的最大最小数只差必为0或1。而sum%n代表的就是差为1的部分;然后加一下所耗的步数就可以了。

实现代码:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; int main()
{
int n,i,a[],ans=,sum=;
cin>>n;
for(i=;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
sort(a+,a+n+);
int p = sum/n;
int n1 = n-sum%n;
for(i=;i<=n1;i++){
if(a[i]>=p)
break;
ans+=p-a[i];
}
for(i=n1+;i<=n;i++){
if(a[i]>p)
break;
ans+=p+-a[i];}
cout<<ans<<endl;
}

Educational Codeforces Round 3 C. Load Balancing的更多相关文章

  1. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  2. CF# Educational Codeforces Round 3 C. Load Balancing

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  4. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  5. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  6. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  8. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  9. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

随机推荐

  1. Windows 系统安装Docker Compose 步骤

    参考 Docker Compose official 官方安装指南: https://docs.docker.com/compose/install/ 实际上到目前为止还不能直接在Windows上安装 ...

  2. odoo 装饰器用法@api

    摘自:blog.csdn.net/cmzhuang/article/details/52932883 @api.one one装饰符自动遍历记录集,把self重新定义成当前记录.注意,返回值是一个li ...

  3. Hive 实现 wordcount

    创建表: create table hive_wordcount(context string); load data local inpath '/home/hadoop/files/hellowo ...

  4. Luogu4099 HEOI2013 SAO 组合、树形DP

    传送门 值得注意的是一般的DAG的拓扑序列数量是NP问题,所以不能直接入手 题目中给出的图可以看做是一个树形图,虽然方向比较迷.考虑使用树形图的性质 不妨任选一个点为根做树形DP,注意到数的位置与方案 ...

  5. Linux下安装解压版(tar.gz)MySQL5.7

            最近尝试在Linux中安装了解压版MySQL,期间查阅了许多博客.很多博客看得我很懵逼,因此记录下自己的安装过程,方便后续查阅.         环境说明:CentOs7.2 一.清理 ...

  6. 设计模式-简单工厂Coding+jdk源码解析

    感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...

  7. [Spark][Python]Spark Join 小例子

    [training@localhost ~]$ hdfs dfs -cat people.json {"name":"Alice","pcode&qu ...

  8. 在asp.net web form项目中添加webapi接口

    我有一个支付宝服务网关是ASP.NET WEB FORM项目,但是最近这个网关需要对外提供几个接口,想了下,使用web api比较合适,实现很简单,GO 1,首先添加一个文件夹名字叫App_Start ...

  9. 通过清华TUNA镜像源下载Android源码

    建议参考官方教程:https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ 以下为自己测试时执行的步骤: 1.下载repo $ curl https://mirr ...

  10. python 批量下载图片

    #coding=utf-8import re,sysimport urllib def getHtml(url): page = urllib.urlopen(url) html = page.rea ...