Lucky Charms
Time limit1sMemory limit128 MB
Given a bracelet of length L nailed at position N, with charms at positions P_i hanging on strings of length S_i, compute how far below the nail each charm droops.
- Level
Medium6 of 10
- Topics
- Geometry, Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Bessie has a lovely charm bracelet of length () mm. Hanging from the bracelet are () charms, each at a unique integer distance from the bracelet's left end. Charm dangles from a string of length mm () that is attached at position () mm from the left end of the bracelet.
Margaret snatches the bracelet and nails it (with a zero-width nail) to a fencepost. The nail is at position () mm from the left end, so the bracelet hangs down on both sides of the nail while gravity pulls the bracelet and every charm straight down.
Bessie wonders: how far below the nail does each charm end up?
As an example, consider a bracelet of length 16 mm with three charms. In the schematic below each + is 1 mm apart and each vertical bar (|) represents 1 mm of string. The three charms hang on strings of length 4, 7, and 3 mm:
1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | |
| | |
| | *
* |
|
|
*
When the bracelet is nailed at position 5, it droops like this (ignore the left-right spread, which is drawn only for clarity):
Droop Bracelet Bracelet
dist. location location
0 5 + 5 <---- nail is here
1 4 + + 6
2 3 | + + 7
3 2 | + + 8 D
4 1 | + + 9 O
5 0 * + + | 10 W
6 + | 11 N
7 + | 12 |
8 + | 13 |
9 + | 14 V
10 + | 15
11 + * 16
12 |
13 |
14 *
The first charm droops 5 mm below the nail, the second 11 mm, and the third 14 mm. Compute the droop distance of every charm.
Input
- Line 1: three space-separated integers , , and .
- Lines 2 to : line describes charm with two space-separated integers and .
Output
- Lines 1 to : line contains the distance from charm to the nail.