×

MCO16206 - Editorial

Author: Zi Song Yeoh

EASY-MEDIUM

PREREQUISITES:

CYCLE-FINDING, BINARY SEARCH, SPARSE TABLE

PROBLEM:

There is a country with $n$ cities arranged in a circle. The length of the path connecting city $i$ and $(i + 1)$ mod $n$ is $l_{i}$. You start at city $1$. Each day, you can run at most $x$ kilometres. However, after each day, you must end up in one of the cities. Determine the minimum number of days before you run at least $y$ kilometres.

EXPLANATION:

This problem can be solved in different ways. Some of the ways which are unoptimized can lead to a solution for the smaller subtasks. I'll describe a full solution here.

Firstly, we can detect if the answer is $-1$ by just going one round and see if we get stuck forever at one town (the length of road for next town is higher than $x$). Thus, from now on, we assume the task is always possible.

Suppose we're at city $i$, which city is the next that we'll visit? We can calculate how many rounds we go from city $i$. Then, we can binary search to find the last town we'll arrive at. We can do this for each city in $O(n\log n)$ total. Now, the key is that if we start from town $1$, and keep moving, eventually after at most $n + 1$ days, we'll end up at one of the towns we've visited before. Thus, we now have a cycle, i.e. after every few days we'll return back to the same position.

Thus, we can divide $y$ by the total length travelled for each cycle and calculate the number of cycles we have to make. Then, we can calculate the number of days needed to finish the last few kilometres of the whole run. This gives us an $O(n \log n)$ solution for this problem. You can see the details for this solution from the tester's solution.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

6★zscoder
62813
accept rate: 6%

19.8k350498541

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,684
×1,672
×1,040
×33
×1

question asked: 19 Dec '16, 09:57

question was seen: 685 times

last updated: 13 Jan '17, 15:33