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. [01-01] 示例:用Java爬取新闻

    1.分析url <空港双流>数字报刊,访问地址为:http://epaper.slnews.net.cn,现在为了抓取每篇新闻的网页内容. 在浏览器访问该链接后,发现链接出现了变化,看样子 ...

  2. 使用awk按照行数切割文件

    最近在做一个事情,需要将一个文本文件按照行数进行切割,然后用了,awk的方法,感觉很好用, 记录一下. 脚本如下: #!/bin/bash ## 文件效果: 根据行数来切割文件 ## 参数1为要切割的 ...

  3. [Oralce][InMemory]如何确定一个表已经被Populate 到In Memory 中?

    [Oralce][InMemory]如何确定一个表已经被Populate 到In Memory 中? 以如下方法来查看 POPULATE_STATUS 是不行的. SQL> select ins ...

  4. Docker(三):Dockerfile 命令详解

    上一篇文章Docker(二):Dockerfile 使用介绍介绍了 Dockerfile 的使用,这篇文章我们来继续了解 Dockerfile ,学习 Dockerfile 各种命令的使用. Dock ...

  5. Pycharm: 代码跳转如何回退 (小技巧)

    背景 玩Python已经有段时间了, 一般都是通过vim和Pycharm来开发, 真心觉得这两个是神器. Vim神器暂且不说, 今天来分享Pycharm的一个小技巧. 用Pycharm的童鞋都知道, ...

  6. 因写太多 BUG!程序员遭公司颁奖羞辱,做的一个比一个绝​

    刚入职的程序员新人,办公桌上,基本上也就一电脑.一键盘.一鼠标,再配个被杯子.然而混迹职场多年的猿老们,办公桌上都有一些彰显身份地位的“好东西”. 这张图两点颇多,最显眼的,是办公桌上那个黄黄的东西, ...

  7. SSO单点登录_理解

    SSO核心意义就一句话:一处登录,处处登录:一处注销,处处注销.即:在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很多人容易把SSO与OAuth搞混.这里简单说明一下: OA ...

  8. Spring AOP不起作用原因

    一.直接在切面类定义切点: AOP切面类里面的方法全部不支持触发切面,否则一个切面函数把自己当做切点就会导致递归层层调用. AOP切面类发出函数调用一律不触发切面,避免两个切面类相互调用迭代请求的情况 ...

  9. LINUX内核设计第五周——扒开系统调用的三层皮(下)

  10. 广商博客冲刺第六七天new

    第四五天沖刺傳送門 第一版的網頁已經放到 云服務器(估計快到期了) 傳送門. (不怎么會玩服務器啊..求指教..目前問題如下: 1.我的電腦mysql密碼跟服務器的密碼不一樣..上傳的時候要把代碼里面 ...