Fish Tank Arrangement
Time limit2sMemory limit1024 MB
Simulate the repeated fish addition, folding, and fish transfer steps until the maximum and minimum fish counts differ by at most K.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
The wizard shark wants to arrange fish tanks with the magic it has learned. Each fish tank is a cube with side length 1, and the shark has fish tanks. At first, the tanks lie in a single row on the floor, and each tank contains at least one fish. Figure 1 shows 8 tanks in a row on the floor. The number in each square is the number of fish in that tank. For convenience, tanks are drawn as squares.

The process of arranging the tanks once is as follows.
First, put one fish into the tank with the fewest fish. If there are several such tanks, put one fish into every tank with the fewest fish. In Figure 1, the tanks with the fewest fish contain 2 fish each, and there are 2 such tanks. Putting one fish into each of the two tanks gives Figure 2.

Now stack the tanks. First, place the leftmost tank on top of the tank to its right, as in Figure 3.

Next, lift all tanks stacked at least two high into the air, rotate the whole lifted group 90 degrees clockwise, and place it on top of the tanks on the floor. The leftmost lifted tank must be above the leftmost tank on the floor. Repeat this operation as long as there is a floor tank below the rightmost lifted tank.
First the arrangement changes as in Figure 4, and another application gives Figure 5.


In Figure 5, it is impossible to lift the tanks once more. As shown in Figure 6, there is no floor tank below the rightmost lifted tank.

After all lifting is finished, adjust the number of fish in the tanks.
For every pair of adjacent tanks, find the difference in fish counts. Let be the quotient when this difference is divided by 5. If is greater than 0, move fish from the tank with more fish to the tank with fewer fish. This process happens simultaneously for all adjacent cells. When it finishes, the arrangement is as in Figure 7.

Now the tanks must be placed back on the floor in a single row. Place them starting from the leftmost tank, and within each position from the bottom tank to the top tank. Figure 8 shows the tanks placed back in a single row.

Perform the lifting operation again. This time, with the center as the boundary, lift the left tanks, rotate the whole lifted group 180 degrees clockwise, and place it on top of the right tanks. Repeat this operation twice. After two repetitions, the number of tanks on the floor is . Figure 9 shows the result after one operation, and Figure 10 shows the result after one more.


Here, perform the fish adjustment described above again, then place the tanks on the floor in a single row. Applying the adjustment to Figure 10 gives Figure 11, and placing the tanks in a single row from Figure 11 gives Figure 12.


Given the number of tanks and the number of fish in each tank, find how many times the tanks must be arranged so that the difference between the maximum and minimum fish counts is at most .
Input
The first line gives and . The second line gives the number of fish in each tank, from the leftmost tank to the rightmost tank.
Output
Print how many times the tanks must be arranged so that the difference between the maximum and minimum fish counts is at most .
Constraints
- is a multiple of 4.
- Each fish tank contains at least 1 and at most 10,000 fish.