USACO Gold 2016 January - Radio Contact
Authors: Neo Wang, Melody Yu
Video Solution
Note: The video solution might not be the same as other solutions. Code in C++.
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++
Code Snippet: C++ Short Template (Click to expand)int N, M;const int INF = 1e9 + 7;const int MX = 1e3 + 1;int sq(int a) { return a * a; }int dist(pi a, pi b) { // distance squared between two points
Java
import java.io.*;import java.util.*;public class radio {public static int[][] fjLocs;public static int[][] bLocs;public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("radio.in"));PrintWriter pw = new PrintWriter(new FileWriter("radio.out"));
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!