Python · June 28, 2022 0

CCC ’14 S3 – The Geneva Confection

https://dmoj.ca/problem/ccc14s3

The problem above is mainly based off logic. It really requires only a few if else statements, variables and loops.

First, you need to create a loop that loops based of an integer input. Everything else will be in this loop.

Create an empty list that is for the numbers of the railway cars. Add the numbers.

Make a number (I’ll call it i). This will be very useful in the future if else statements. Create another variable that is the length of the cars on the mountain top. I’ll call it b.

Then, create a while loop, under the condition where i<b, or length of the branch isn’t 0.

Make the main if else:

if mountain top could be moved to the lake, then move it.

elif the branch could be moved to the lake, then move it.

elif the mountain top could be moved to the top, then move it.

else, then break from the loop.

If the first two if else parts are run, then i = i + 1.

Sample solution (python 3):

for _ in range(int(input())):
    mountain_top = [int(input()) for __ in range(int(input()))]
    b, geneva_lake, branch, i = len(mountain_top), [], [], 0
    while i < b or len(branch) != 0:
        if len(mountain_top) > 0 and i + 1 == mountain_top[-1]:
            moving_car = mountain_top.pop()
            geneva_lake.append(moving_car)
            i += 1
        elif len(branch) > 0 and i + 1 == branch[-1]:
            moving_car = branch.pop()
            geneva_lake.append(moving_car)
            i += 1
        elif len(mountain_top) > 0:
            moving_car = mountain_top.pop()
            branch.append(moving_car)
        else:
            break
    print('Y' if len(geneva_lake) == b else 'N')