Babbling Brooks
InterviewTime limit1sMemory limit128 MB
Simulate a sequence of splits and joins on a list of streams and print the resulting flows after each split divides flow by a percentage and each join merges neighbors.
- Level
Easy3 of 10
- Topics
- Implementation, Simulation, Array
- Solved
- No attempts yet
Problem
A series of streams runs down the side of a mountain. The mountainside is very rocky, so the streams split and rejoin many times. At the foot of the mountain, several streams emerge as rivers. Your job is to compute how much water flows in each river.
At any given elevation there are streams, labelled to from left to right. As we proceed down the mountainside, one stream may split into a left fork and a right fork, increasing the total number of streams by , or two streams may rejoin, reducing the total number of streams by . After a split or a rejoining occurs, the streams are renumbered consecutively from left to right. There is always at least one stream, and there are never more than streams.
Input
The first line contains , the initial number of streams at some high altitude. The next lines give the flow in each stream from left to right. Proceeding down the mountainside, several split or join locations are encountered.
Each split location is described by three lines:
- a line containing
99(to indicate a split) - a line containing an integer, the number of the stream that is split
- a line containing an integer between and , the percentage of the split stream's flow that goes to the left fork (the rest goes to the right fork)
Each join location is described by two lines:
- a line containing
88(to indicate a join) - a line containing an integer, the number of the stream that is rejoined with the stream to its right
The flow from both joined streams is combined. After the last split or join location, a single line containing 77 marks the end of input.
Output
Determine how many streams emerge at the foot of the mountain and what the flow is in each. Output the flow in rivers through , each rounded to the nearest integer (rounding a value of exactly up), separated by single spaces on one line.