Jenga Boom
Time limit2sMemory limit512 MB
Simulate removals from a Jenga-like tower and report whether it falls, and at which removal, when a level's center of mass leaves the convex hull of the blocks still supporting it.
- Level
Hard8 of 10
- Topics
- Geometry, Simulation, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Jane is a game designer and she is working on the next version of Jenga Boom. A block in this version measures instead of the usual . The tower is built when the game starts. Each level holds blocks laid side by side along their long sides, and every level sits at a right angle to the level below it. Players take turns pulling out one block at a time, and the game ends when the tower falls.

The initial tower

The tower just before it falls
Jane wants a simulator that helps her choose and . The simulator computes when the tower falls if the blocks are pulled out in a given order.
Set up coordinates as follows. The ground is the plane and level occupies the slab . The footprint of a level is the square . In an odd numbered level the long side of a block runs along the axis and block occupies . In an even numbered level the long side runs along the axis and block occupies . Every block has the same material and the same mass.
Consider the cross-section between level and level . If at least one block is left in levels through , let be the center of mass of those blocks projected onto the plane, and let be the convex hull of the projection of the blocks left in level . The tower falls if lies outside or on the boundary of . If level is empty then is empty too, so the tower falls as long as a block is left above it. If any cross-section with meets this condition, the tower falls right after that block is pulled out.
Input
The first line contains two integers and that define the size of a block (). The second line contains two integers and , the number of levels in the tower and the number of blocks that are pulled out ().
Each of the next lines describes one removal, in order, with two integers and : the level counted from the bottom and the position of the block inside that level (, ). No block is pulled out twice.
Output
Print yes on the first line if the tower falls, and no otherwise. If it falls, print on the second line which removal brought it down, counted from 1. Ignore the input that is left after the tower falls.