Bead Sort
Time limit2sMemory limit1024 MB
For each prefix of b, output the total bead travel distance of bead sort on that prefix, modulo 1000000007.
- Level
Medium7 of 10
- Topics
- Segment tree, Sorting, Math
- Solved
- No attempts yet
Problem
Bead sort (also called gravity sort) sorts an array of positive integers by threading beads onto vertical rods and letting gravity pull them down. Implementing it on a computer has many difficulties, such as space complexity, but in a realistic physical model it is known to run in roughly time. Bead sort proceeds exactly as follows.
- Let be the largest value in the sequence. Prepare rods and number them 1 to . Initially the rods lie flat.
- Divide each rod into cells, where the length of one cell equals the diameter of a bead. From this point on, write "the -th cell of the -th rod" as .
- For each element of the sequence, thread a total of beads, from through .
- Stand all rods up perpendicular to the ground at the same time, with cell 1 at the top. Gravity pulls each bead toward the higher-numbered cells. Then count the beads in the -th cell from the top. These counts match the elements of the sorted array.
Bead sort therefore uses beads in total. For an array of positive integers, let be the total distance moved by all beads when bead sort is run on , measured in cells. Given an array of length , compute for each from 1 to , where is the prefix of length . Since the answer can be large, print it modulo .
Input
The first line contains the length of the array ().
The second line contains the positive integers through , separated by spaces. ()
Output
Print lines. On the -th line, print the value of . Since the answer can be large, print it modulo .