Balance of Boxes
Time limit1sMemory limit256 MB
Given box centers stacked from the floor, decide whether every suffix's center of mass lies strictly inside the box just below it.
- Level
Medium5 of 10
- Topics
- Array, Prefix sum, Implementation, Math
- Solved
- No attempts yet
Problem
Jinsu has a total of n boxes. Every box is a square of size 2L × 2L, and each box has uniform density.
Jinsu stacks these boxes one on top of another starting from the floor. The floor is at y=0.
Numbering the boxes 1, 2, ..., n from the one closest to the floor, the center of box i is (xi, 2L×i - L), which is the same as the center of mass of box i alone.
Since each box has uniform density, the center of mass of several boxes is the average of the centers of mass of the individual boxes.
Jinsu wants to know whether the boxes stay balanced without collapsing when stacked at the desired center coordinates.
For every 1 ≤ i < n, if the x-coordinate of the center of mass of boxes i+1, i+2, ..., n lies inside the interval of box i, the whole stack is balanced. The interval of box i is defined as the open interval between xi-L and xi+L, excluding xi-L and xi+L. Therefore a center of mass that lies exactly on a corner of a box does not count as balanced.
Given the center coordinates of n boxes, determine whether those boxes are balanced.
Input
The first line gives the number of boxes n (1 ≤ n ≤ 200,000) and the box size L (1 ≤ L ≤ 109).
The second line gives the x-coordinates of the centers of mass x1, x2, ..., xn (-109 ≤ xi ≤ 109) that Jinsu wants.
Output
On the first line, print "stable" if the boxes are balanced, or "unstable" otherwise.