Problem solution · C++

CCC 2019 S4 - Tourism

CCC 2019 S4 - Tourism: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Dynamic programming
Source
CCCSolutions
Length
69 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For CCC 2019 S4 - Tourism, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 69 lines of C++ from the credited upstream file ccc19s4.cpp.
  • The implementation visibly relies on work queue, cached states.
  • 9 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2019 S4 - Tourism · C++C++
Use this to learn the idea, then write your own version.
// Daniel Zhang, Pinetree Secondary School// Editorial for this solution available at https://dmoj.ca/user/sinsane/  #include <bits/stdc++.h> using namespace std; using ll = long long; ll arr[1000015], dp[1000015], st[1000015][20]; //queries max from l, r inclusivell rmq(ll l, ll r) {    if (l > r) return 0;    ll length = r - l + 1, k = 31 - __builtin_clz(length);    return max(st[l][k], st[r-(1<<k)+1][k]);} int main() {    ios_base::sync_with_stdio(false); cin.tie(0);     ll n, k; cin >> n >> k;     for (ll i=1; i<=n; i++) {cin >> arr[i]; st[i][0] = arr[i];}     if (k == 1) {        cout << accumulate(arr, arr + n + 1, 0LL) << endl;        return 0;    }    //Build max sparse table (errichto, should be correct)    for (int m = 1; m < 20; m++) {        for (int i = 1; i + (1<<m)-1 <= n; i++) {            st[i][m] = max(st[i][m-1], st[i+(1<<(m-1))][m-1]);        }    }        deque<ll> red; red.push_back(0);    deque<ll> blue; blue.push_back(0);     int blockStart = 1;    for (int i=1; i<=n; i++) {        if (i % k == 1 && i != 1) { //new day block            blockStart = i;            red.clear(); blue.clear();             for (int j=i-k; j<i; j++) { //add all previous day transitions into monoqueues                while (!red.empty() && dp[red.back()] <= dp[j]) red.pop_back();                red.push_back(j);                 while (!blue.empty() && dp[blue.back()] + rmq(blue.back()+1, i-1) <= dp[j] + rmq(j+1, i-1)) blue.pop_back();                blue.push_back(j);            }            //cout << red << endl;            //cout << blue << endl;        }                ll withRed = dp[red.front()] + rmq(blockStart, i);        ll withBlue = dp[blue.front()] + rmq(blue.front()+1, i);        dp[i] = max(withRed, withBlue);                //trim monoqueues        while (!red.empty() && red.front() <= i-k) red.pop_front();        while (!blue.empty() && blue.front() <= i-k) blue.pop_front();    }         cout << dp[n] << endl;}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗