From Gleneagle Secondary School, Coding Bowl
About
Click to make CCO: https://cccsolutions.ca/
Tourism Editorial
Author: sinsane | Original solution: kuruluu
This editorial will cover one of the multiple ways to solve this problem.
Subtask 1 (
)
As
Time Complexity:
Subtask 2 (
)
We can use dynamic programming to solve this subtask. Let
Time Complexity:
Full Solution:
Our current transition time is too slow. We can use two monoqueues: red
and blue
, to get the optimal
A = 2 5 7 | 1 4
The critical observation is that once we enter a new "block" of days (in this case attractions
- For the best
to transition from, the maximum is in the current block of days. - For the best
to transition from, the maximum is in the previous block of days.
For the first case, let the red monoqueue store only the
For the second case, let the blue monoqueue store only the
The value of
To maintain the monoqueues, clear them upon entering a new block of days, and insert all of the indices in the previous block. In our example, upon entering the new block at attraction
Psuedocode:
initialize monoqueues red, blue
for i -> 1, 2, .. N:
if newBlock:
clear monoqueues
for j -> i-k, .. i-1:
push j into red, comparing dp[red.back] and dp[j]
push j into blue, comparing dp[blue.back] + rmq(blue.back+1, i-1) and dp[j], rmq(j+1, i-1)
dp[i] = max(redScore, blueScore)
pop expired elements from monoqueues
Time Complexity:
- 169p on Dec 6 2022
- 300p on April 22 2024
- 400p on June 2 2024
- Tourism slain on Sep 1, 2024 (thanks kuruluu)
- Purple on Feb 17
- 500p (?)
- CCO (?)
Editorials: https://dmoj.ca/user/KurbyDoo