## CCC '00 S2 - Babbling Brooks

View as PDF

Points: 7
Time limit: 1.0s
Memory limit: 256M

Problem type
##### Canadian Computing Competition: 2000 Stage 1, Junior #4, Senior #2

A series of streams run 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 of the streams 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 Specification

The first line of input contains , the initial number of streams at some high altitude. The next lines give the flow in each of the streams from left-to-right. Proceeding down the mountainside, several split or rejoin locations are encountered. For each split location, there will be three lines of input.

• 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 flow from the split stream that flows to the left fork. (The rest flows to the right fork)

For each join location, there will be two lines of input:

• 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 will be:

• a single line containing 77 (to indicate end of input)

#### Output Specification

Your job is to determine how many streams emerge at the foot of the mountain and what the flow is in each. Your output is a sequence of real numbers, rounded to the nearest integer, giving the flow in rivers through .

#### Sample Input

3
10
20
30
99
1
50
88
3
88
2
77

#### Sample Output

5 55

• commented on Dec. 28, 2022, 2:22 p.m.

Use the round function at the end of your output to solve test case 5. This one was a relatively easy 7 points.

• commented on June 22, 2022, 5:32 p.m.

I got an IndexErroron the last test case (I use Python). Can anyone tell me what might be causing this?

• commented on Jan. 4, 2023, 11:04 p.m. edit 2

I had a similar issue. The problem was that my code was detecting the flow percentage values that are 99 or 88 as an indicator to start another split/rejoin.

For example:

splits_and_rejoins = [99, 2, 88, 88, 99, 77]

This list represents a split at river #2 that separates the river into one that flows at a percentage of 88% and a rejoin at river 99 and the one to its right. However, your code might interpret it as a rejoin at the third index and a split at the fifth index.

Try using some try/excepts and print statements to locate your issue, and add an if statement if that indeed is your problem.

Hope that helped!

• commented on June 20, 2022, 4:02 p.m.

I'm not quite understanding why the 5th test fails. all stream nos are handled in float and just rounded at the output. Help would be appreciated.

my submission is here

• commented on Dec. 8, 2022, 5:33 p.m.

Did you figure it out? mine is not working too

• commented on May 12, 2022, 7:29 p.m. edit 3

Hey, I'm not sure why my code fails test cases #4 and #5. I thought it was an issue where the m stream would not join back with the first stream, but it appears not. Could someone take a look at it and tell me what is wrong?

Edit: I found the issue. Upon splitting, both branches of the stream would contain an equal flow rather than the left having the inputted percentage and the right having the remaining.

• commented on May 7, 2022, 2:53 a.m.

Same here . Can't figure out why the last test is failing ??

• commented on April 19, 2022, 5:28 p.m.

I'm having trouble with the last test case. I made sure that rounding was my final step but I must be missing something or rounding incorrectly I think. Any ideas?

• commented on Feb. 5, 2022, 9:11 p.m.

remember that you only round at the very end

• commented on Sept. 15, 2021, 12:03 p.m.

Can anyone explain to me why the output stream is 5? after 1 split and 2 rejoin? thz

• commented on Nov. 26, 2021, 12:21 p.m. edit 2

rivers at first:
10 20 30

after 1st split:
5 5 20 30
(1st river is split 50:50)

after 1st join:
5 5 50
(3rd river combines with the river to the right)

after 2nd join:
5 55
(2nd river...)

so the final outcome is:
5 55

• commented on July 13, 2020, 4:48 p.m.

When splitting or merging, left and right is based on your view of the mountain (i.e., the direction is not relative to the split).

• commented on Feb. 8, 2020, 4:25 p.m. edited

The first 3 cases got accepted, but I can't quite figure out what's wrong with the last one. Could someone help? (I use Python)

My submission

• commented on Jan. 13, 2021, 11:53 p.m.

Line 29 - Integer divided by an integer returns integer, flooring the value before you intend to round it.

• commented on July 10, 2019, 5:20 p.m. edit 3

Getting all test cases right except for the last one. Can't find any issues in my code. Anyone have any ideas?

Edit: Found my issue

• commented on Jan. 18, 2020, 6:09 p.m. edit 2

I have the same issue. How did you fix yours?

Edit: The problem was that, when a stream splits, the amount of water in it could be a decimal.

• commented on Feb. 7, 2022, 9:15 p.m.

Hi - I think I am having the same problem - do i need to make use of float somewhere in my if loop for 99?

• commented on Feb. 8, 2022, 10:35 p.m.

im pretty sure you only round at the end