Educational Codeforces Round 3 C. Load Balancing
2 seconds
256 megabytes
standard input
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.
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.
Print the minimum number of seconds required to balance the load.
2
1 6
2
7
10 11 10 11 10 11 11
0
5
1 2 3 4 5
3
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:
- move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5);
- then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4);
- 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的更多相关文章
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- 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 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
随机推荐
- 7-51单片机ESP8266学习-AT指令(8266TCP服务器,编写自己的C#TCP客户端发信息给单片机控制小灯的亮灭)
http://www.cnblogs.com/yangfengwu/p/8780182.html 自己都是现做现写,如果想知道最终实现的功能,请看最后 先把源码和资料链接放到这里 链接: https: ...
- mysqldump备份数据库
1. 备份数据库-->sql文件 mysqldump -h192.168.1.100 -uuser -p123 --databases name > /tmp/databasedump.s ...
- C# 16进制转 Brush 颜色对象
原文:C# 16进制转 Brush 颜色对象 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014117094/article/details/4 ...
- 多线程-volatile关键字和ThreadLocal
1.并发编程中的三个概念 原子性:一个或多个操作.要么全部执行完成并且执行过程不会被打断,要么不执行.最常见的例子:i++/i--操作.不是原子性操作,如果不做好同步性就容易造成线程安全问题. 可见性 ...
- 【LGR-047】洛谷5月月赛
这次我期待了很久的Luogu月赛崩掉了 传说中的Luogu神机就这样被卡爆了 然后我过了20min才登上Luogu的网站,30min后才看到题目 然后交T1TM的不给我测!!!然后又交了一次机子就炸了 ...
- [Partition][Index]对于Partition表而言,是否Global Index 和 Local Index 可以针对同一个字段建立?
对于Partition表而言,是否Global Index 和 Local Index 可以针对同一个字段建立? 实验证明,对单独的列而言,要么建立 Global Index, 要么建立 Local ...
- MGR主从不一致问题排查与修复
运行环境 linux:CentOS release 6.8 (Final) kernel:2.6.32-642.6.2.el6.x86_64 mysql Server version: 5.7.21- ...
- Java中clone的写法
Cloneable这个接口设计得十分奇葩,不符合正常人的使用习惯,然而用这个接口的人很多也很有必要,所以还是有必要了解一下这套扭曲的机制.以下内容来自于对Effective Java ed 2. it ...
- 用 Python 分析咪蒙1013篇文章,她凭什么会火?
咪蒙 文学硕士,驾驭文字能力极强.并且是一个拥有一千多万粉丝,每篇文章阅读量都 100W+,头条发个软文都能赚 80 万,永远都能抓住粉丝G点的那个女人. 1月份因为某篇文章,在网络上被一大批网友 ...
- Linux下批量ping某个网段ip的脚本
比如现在需要对172.16.50.0/24网段的ip进行检查,检查哪些ip现在被占用,哪些ip没有被占用,可以通过ping命令来检查,脚本如下: [root@uatdns01 opt]# vim /o ...