USACO Gold 2016 January - Radio Contact
Author: Neo Wang
Appears In
Explanation
The key observation to make here is already stated in the problem statement: "Farmer John can either stay put at his current location, or take one step forward", and likewise for Bessie. Thus, when constructing our , we can set equal to the best distance at Farmer John's move and Bessie's move .
Notice that it is hard to calculate a cumulative distance if we leave the string unprocessed (meaning that we read from the string directly). To resolve this, we can simply calculate the coordinates at every step of Bessie and Farmer John's path. In our implementation, we map each character to their appropriate dx
, dy
arrays in order to apply the appropriate changes, storing Farmer John's position at move as and Bessie's position at move at .
After this is done, we start building our :
Either one of the following happens:
Farmer John takes a step :
Bessie takes a step :
Both Farmer John and Bessie take steps and :
Implementation
Time Complexity:
C++
#include <bits/stdc++.h> // see /general/running-code-locallyusing namespace std;using ll = long long;using vi = vector<int>;#define pb push_back#define all(x) begin(x), end(x)#define sz(x) (int)(x).size()
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!