Editorial for Baltic OI '19 P2 - Nautilus
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
(?
queries)
The solution to this subtask is straightforward — try all possible starting positions. It is simple to verify whether the ship could have started at a given position: simply simulate the ship's movement, starting from that position. If you hit an edge or island, you know that the ship couldn't have started there.
Verifying this for each position takes
Subtask 2
(
We solve this using dynamic programming. We want
We can make some observations. If there is an island at position E
, then
Using these relations, we can calculate
The complexity is once again
Subtask 3
(
The complexity shall still be bitset
is a data type that can be thought of as an array of booleans that supports, among other things, the following operations: bitwise "or" (denoted
The bitwise shifts shift the whole string by an integer number of positions, appending or prepending zeroes if necessary. For example:
We represent .
is converted to #
is converted to
Cases S
and W
are handled analogously.
How to do this in Java or Python? In Python, the jury simply used Python's big integers and treated them as bitstrings. In Java, both the BigInteger
and BitSet
classes exist, but neither work for this problem: BigInteger
is too slow and BitSet
lacks the bit-shift operators that we require. Instead, we replaced each bitset with a short array of long
s.
In fact, there are other ways to optimize the C++ solution. For example including the following line:
#pragma GCC optimize("Ofast")
will include optimizations hardcore enough to be comparable with bitsets.
Credits
- Task: Marijonas Petrauskas (Lithuania)
- Solutions and tests: Tähvend Uustalu, Andres Unt (Estonia)
Comments