Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic Ship Editorial #5099

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion solutions/silver/cf-1117C.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
id: cf-1117C
source: CF
title: Magic Ship
author: Kevin Sheng
author: Kevin Sheng, David Guo
---

[Official Editorial (C++)](https://codeforces.com/blog/entry/65365)

## Explanation

We start by calculating how the wind shifts us over a given number of days, and then we check whether we can cover the remaining distance with our own daily movement in that same amount of time. Specifically, we figure out the net change of one full cycle of wind, then multiply that by how many full cycles fit into our candidate number of days, and finally account for any leftover days by finding the partial cycle change. Note that the partial cycle change will take more days, which is fine as we can always reach our destination and continue counteracting the wind.

If we know the candidate number of days we are traveling for, we can measure the total change from the wind by calculating how many full cycles of wind fit within our number of days. We apply that shift to our initial position and then measure the distance to the destination. If the Manhattan distance to the destination is no greater than the number of days, it means we could have used some of those days to make up the distance with our own movements, so reaching the destination is possible by that day.

Since the answer is monotonic and knowing if a candidate number of days lets us reach our destination in time, we then perform a binary search on the number of days to find the smallest time for which this reachability check is true. Finally, we output the smallest valid day or conclude that it is impossible, in which case we output $-1$.
JoltedCowIceCream marked this conversation as resolved.
Show resolved Hide resolved

## Implementation

<LanguageSection>
Expand Down
Loading