BALIFE - Load Balancing

no tags 

SuperComputer Inc. have built a super-fast computer server consisting of N hyper-scalar lightning-fast processors Beta 007. These processors are numbered from 1 to N and are used to process independent jobs. Every new incoming job is assigned to an arbitrary processor. Sometimes, a processor may be assigned too many jobs while other processors have a relatively light load (or even wait idly). In that case, the whole system undergoes rebalancing.

Rebalancing proceeds in rounds. In each round, every processor can transfer at most one job to each of its neighbors on the bus. Neighbors of the processor i are the processors i-1 and i+1 (processors 1 and N have only one neighbor each, 2 and N-1 respectively). The goal of rebalancing is to achieve that all processors have the same number of jobs.

Given the number of jobs initially assigned to each processor, you are asked to determine the minimal number of rounds needed to achieve the state when every processor has the same number of jobs, or to determine that such rebalancing is not possible.

Input file specification

The input file consists of several blocks. Each block begins with a line containing a single number N(1<= N <=9000) - the number of processors. N numbers follow, separated by spaces and/or end of line characters. The i-th number denotes the number of jobs assigned to the i-th processor before rebalancing. There is a blank line after each block. The last block is followed by a single number -1 on a separate line (which should not be processed).

Output file specification

For each block in the input file, output the minimal number of rounds needed to rebalance loads for all the processors. If it is not possible to rebalance jobs so that each processor has the same number of jobs, output -1.

Example

Input file:
3
0 99 3

2
49 50

8
16 17 15 0 20 1 1 2

10
0 0 100 0 0 0 0 0 0 0

-1

Output file:
34
-1
23
70

hide comments
anshuman1117: 2018-08-12 05:45:29

Very easy to get AC in one go! Few hints:

1. Make prefix sum of main and prefix sum of average array.
2. Take the maximum absolute difference between them, that is the answer.
3. Check if the average is divisible by n, if not then it can obviously not be rebalanced.
4. Take special care of the -1 at the end of the input.
5. Constraints are, n <= 111111.

Happy coding :)

Last edit: 2018-08-12 05:47:19
mag1x_: 2018-06-22 13:59:42

Prefix Sum of initial array and balanced array :)

aryan12: 2018-05-23 14:08:17

AC in 3 chances....just because of a silly mistake, putting cin after the condition to break the loop.
By the way a nice question and the -1 part is a little tricky.Future coders please take care about the -1 at the end and it should not give a result!!!

Last edit: 2018-05-23 19:39:59
techobist: 2018-05-16 21:56:11

"In case it's not clear, a processor in the middle doesn't have to transfer one job to each neighbour. It can choose in each round not to transfer, to transfer to only one neighbour, or to transfer to both neighbours."
Thanks @Brian

egoista_: 2018-04-28 09:24:29

O(n) solution, AC in one go. Just find the prefix sum of the given array and prefix sum of another array which have all the elements as average. Maximum difference between corresponding positions in these two arrays is the answer.

nadstratosfer: 2017-11-25 05:28:53

Uber-retarded testfile format, can't process it in Python. Everything I could throw at it resulted in NZEC, even with exception handling. Anyone succeeds reading through this garbage, please post the method that worked.

doluongk56: 2016-12-05 05:39:12

O(n) - AC in one go.

detel: 2015-07-18 23:29:57

Try not to use string reading input for languages like Python/Java as there's an extra EOF at the end of number of jobs!!!

Brian Bi: 2014-05-03 06:40:07

In case it's not clear, a processor in the middle doesn't have to transfer one job to each neighbour. It can choose in each round not to transfer, to transfer to only one neighbour, or to transfer to both neighbours.

Brian Bi: 2014-05-02 23:02:03

Problem description should specify bounds on # of jobs initially assigned to each processor. (Looking at original test data from IPSC, it seems the maximum value is 10153)


Added by:Fudan University Problem Setters
Date:2007-11-03
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:IPSC 2002