#C7133. Redistribution of Marbles
Redistribution of Marbles
Redistribution of Marbles
You are given N containers, each containing some marbles. Your task is to determine whether it is possible to redistribute the marbles so that every container ends with the same number of marbles. In one move, you can transfer one marble from one container to an adjacent container (i.e., container i to container i+1 or vice versa). If the total number of marbles is not divisible by N, redistribution is impossible.
If redistribution is possible, output "YES" along with a sequence of moves that will lead to all containers having equal marbles. Otherwise, output "NO". When printing the moves, first print the number of moves, and then list each move as a pair of integers denoting the source container and the destination container. Note that container indices are 1-indexed.
The rebalancing process should follow this procedure:
- Compute the total number of marbles and the target number for each container, i.e. \( target = \frac{\text{total marbles}}{N} \).
- If redistribution is possible, iterate through the containers (from the first to the last) and use adjacent moves to adjust the number of marbles between container i and container i+1.
It is guaranteed that if a solution exists, one can be found by repeatedly transferring marbles between adjacent containers.
inputFormat
The input is given from standard input (stdin) and consists of two lines:
- The first line contains a single integer \( N \) (the number of containers).
- The second line contains \( N \) space-separated integers, where the \( i\)-th integer is the number of marbles in the \( i\)-th container.
outputFormat
If redistribution is impossible, output a single line with "NO".
If redistribution is possible, output the following to standard output (stdout):
- The first line should contain "YES".
- The second line should contain an integer \( M \), representing the number of moves.
- Each of the following \( M \) lines should contain two space-separated integers, representing a move from the source container to the destination container.
3
1 2 3
YES
2
2 1
3 2
</p>