Sharing the water between cells
Statement
There is a reservoir where water cells are joined into a grid that is N cells wide and N cells tall. Each water cell has the amount of water it holds right now written on it as a natural number. Between two water cells that touch side by side or top to bottom there is a partition, and opening a partition lets the water of the two cells mix.
The keeper repeats the following work, one round at a time.
1) Look at every pair of water cells that touch side by side or top to bottom, and open the partition wherever the difference between the amounts of water they hold is from L to R.
2) Water cells that are connected to one another through open partitions count as one group. A water cell left on its own because no partition around it opened is also a group.
3) For each group, add up the water in every cell of that group, divide the total by the number of cells in the group, and put that much water back into each cell. If the division is not exact, the part below the decimal point is dropped.
4) Close all the partitions.
The amounts compared in step 1 are the amounts at the moment the round begins. Water that already mixed earlier in the same round does not enter the decision to open another partition. Once there is no group holding two or more water cells, no more water moves, so the work stops.
So, given the starting amounts of water, write a program that finds how many rounds in all the water moved in.
For example, suppose N=2, L=20 and R=50, and the starting amounts of water are as in the table below.
| 44 | 78 |
|---|---|
| 12 | 23 |
In the first round the difference between 44 and 78 is 34 and the difference between 44 and 12 is 32, so the two partitions around the top-left cell open. The difference between 12 and 23 is 11, which does not reach L=20, and the difference between 78 and 23 is 55, which goes past R=50, so those two partitions stay shut. The cells holding 44, 78 and 12 are connected through the open partitions, so they become one group and each takes 44, the sum 134 divided by 3, while the cell holding 23 is left on its own.
| 44 | 44 |
|---|---|
| 44 | 23 |
In the second round the cell holding 23 differs by 21 from the 44 above it and by 21 from the 44 to its left, so both partitions open. The other places have a difference of 0, so they stay shut. The three cells connected through the open partitions add up to 111, so each takes 37, and the 44 at the top left is left on its own.
| 44 | 37 |
|---|---|
| 37 | 37 |
In the third round the difference between any two touching cells is only 7 or 0, which does not reach L=20, so not one partition opens. The water therefore moved in two rounds, and the answer in this case is 2.
Input and Output Examples
Example 1
Input
2 20 50 44 78 12 23
Output
2
Example 2
Input
2 20 50 50 30 20 40
Output
1
Example 3
Input
1 1 1 2
Output
0
Input
First the number of water cells along one side of the reservoir N and the two values L and R that decide when a partition opens are given on one line, separated by spaces. N is a natural number less than or equal to 50, and L and R are natural numbers from 1 to 100 with L less than or equal to R.
Then N lines follow, giving the amounts of water in the cells of each row from the top row down. One line holds N values separated by spaces, starting from the left, and each value is a natural number from 1 to 100.
Output
Print on one line how many rounds in all the water moved in. Only the rounds in which at least one group held two or more water cells are counted. If no partition opens from the start, print 0. Only one number is printed, so no space is left at the end of the line.
Requirements
- A partition sits only between two water cells that touch side by side or top to bottom. Cells do not touch diagonally
- The decision to open a partition uses the amounts of water at the moment the round begins. Water that mixed earlier in the same round does not enter the decision
- The water in one group all becomes the total divided by the number of cells, and if the division is not exact the part below the decimal point is dropped
- A round in which at least one group holding two or more water cells appeared counts as one round
- L is 1 or more, so a partition between two cells holding the same amount of water never opens. The work therefore always comes to an end
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 16 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 16 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 16 cases to solve this.
1 1 1 2
0
2 20 50 44 78 12 23
2
2 20 50 50 30 20 40
1
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.