Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

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 Input

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

Sample Output

Hint

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.

Source


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[1000010];
int main()
{
int n;
scanf("%d",&n);
int sum=0;
int maxx,minn;
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
}
minn=maxx=sum/n;
int ans1,ans2;
ans1=ans2=0;
if(sum%n)
maxx++;
for(int i=0;i<n;i++)
{
if(num[i]<minn) ans1+=minn-num[i];
else if(maxx<num[i]) ans2+=num[i]-maxx;
}
int s=max(ans1,ans2);
printf("%d\n",s);
return 0;
}

CodeForces--609C --Load Balancing(水题)的更多相关文章

  1. CodeForces 609C Load Balancing

    先算出目标状态,然后拿当前状态与目标状态对比,即可算出答案 #include<cstdio> #include<cmath> #include<cstring> # ...

  2. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  3. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  4. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces 489A SwapSort (水题)

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  6. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. CodeForces 534B Covered Path (水题)

    题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...

  8. Codeforces Gym 100286I iSharp 水题

    Problem I. iSharpTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  9. CodeForces 705A(训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...

随机推荐

  1. c# 正则表达式regex心得

    5.1. C#中的正则表达式的简介 C#中的Regex类处理正则表达式. 5.2. C#正则表达式的语法 5.3. C#中的正则表达式的特点 下面总结一些C#中的正则表达式相对于其他语言中的正则表达式 ...

  2. First step in troubleshooting complex issues: Define and scope your issue properly

    最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...

  3. CSS简单入门

    - Java攻城狮学习路线 - 一. 什么是CSS CSS指层叠样式表(Cascading Style Sheets),定义如何显示HTML元素 二. CSS语法 /* 选择器 { 声明: 声明:}* ...

  4. Docker的官网在线--中文教程

    1.官网界面:https://www.docker.com/tryit/ In this 10-minute tutorial, see how Docker works first-hand: Yo ...

  5. (转)RabbitMQ学习之安装

    http://blog.csdn.net/zhu_tianwei/article/details/40832185 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客 ...

  6. pycharm安装以及简单使用教程

    https://www.cnblogs.com/jin-xin/articles/9811379.html   以windows版本举例: 1.首先去Pycharm官网,或者直接输入网址:http:/ ...

  7. mysql 读写分离 ,mysql_proxy实现

    下载安装mysql_proxy: 解压后, 该目录包含已经编译好的二进制文件 . 1: 利用mysql_proxy实现负载均衡 执行mysql_proxy ./mysql-proxy-path/bin ...

  8. BitmapMesh动画

    一.概要 我们经常用到Canvas.drawBitmap方法,却很少用到Canvas.drawBitmapMesh方法.这个方法为我们做图片变形提供了无限可能,同时也对数学功底有较高的要求.下面先看一 ...

  9. When you hit a wall, just kick it in.

    Teach Yourself Programming in Ten Years. ----- Peter Norvig Teach Yourself Programming in Ten Years  ...

  10. 莫烦大大TensorFlow学习笔记(8)----优化器

    一.TensorFlow中的优化器 tf.train.GradientDescentOptimizer:梯度下降算法 tf.train.AdadeltaOptimizer tf.train.Adagr ...