CF609C.Load Balancing
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 m__i 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 m__a - m__b, 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.
学校计算机房中有 n 台服务器,负责处理若干计算任务。已知每台服务器上已分配的任务数量:第 i 台服务器上分配了 mi 个任务。
为了均衡各服务器的负载,你希望重新分配部分任务,使得负载最重的服务器与负载最轻的服务器之间的任务数之差尽可能小。换言之,你希望最小化表达式 ma−mb,其中 a 是负载最重的服务器,b 是负载最轻的服务器。
每秒钟你可以重新分配一个任务。即每秒钟你可以任选两台服务器,并将一个任务从其中一台迁移至另一台。
请编写一个程序,求出使服务器负载达到均衡所需的最少秒数。
输入格式
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 _m_1, _m_2, ..., m__n (0 ≤ m__i ≤ 2·104), where m__i is the number of tasks assigned to the i-th server.
第一行包含一个正整数 n(1≤n≤105)—— 服务器的数量。
第二行包含一个由非负整数组成的序列 m1,m2,...,mn(0≤mi≤2⋅104),其中 mi 表示分配给第 i 台服务器的任务数量。
输出格式
Print the minimum number of seconds required to balance the load.
输出平衡负载所需的最少秒数。
输入输出样例
输入#1
2 1 6
输出#1
2
输入#2
7 10 11 10 11 10 11 11
输出#2
0
输入#3
5 1 2 3 4 5
输出#3
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.
在第一个例子中,需要两秒时间。每秒应将服务器 #2 上的一个任务移至服务器 #1。两秒后,服务器 #1 上应有 3 个任务,服务器 #2 上应有 4 个任务。
在第二个例子中,负载已处于均衡状态。
第三个例子中一种可能的任务移动序列为:
- 将服务器 #4 上的一个任务移至服务器 #1(序列 m 变为:2 2 3 3 5);
- 接着将服务器 #5 上的一个任务移至服务器 #1(序列 m 变为:3 2 3 3 4);
- 接着将服务器 #5 上的一个任务移至服务器 #2(序列 m 变为:3 3 3 3 3)。
上述序列是使服务器负载在三秒内达到均衡的若干可行方案之一。
输入解题思路,AI测评打分。不知道怎么写?