Upload
karin-spencer
View
215
Download
2
Embed Size (px)
Citation preview
Houyang
3/25/13
USACO March Contest
• Congrats to Johnny Ho for scoring over 900 points in the Gold Divisiono 7th place in US
• Kudos to Jonathan Uesato too, for a score of 878o Almost slayed the Ho
Bronze: Breed Proximity
• N (1 <= N <= 50000) cows in a line.o Each has a breed (breeds are integers 0...1000000)
• Cows can be crowded:o Two cows of the same breed are said to be
"crowded" if their positions within the line differ by no more than K (1 <= K <= N)
• Find the maximum breed of a pair of crowded cows.
• Sample: N = 6, K = 3
• Breeds: 7 3 4 2 3 4
• Output: 4
Bronze: Breed ProximitySolution
• Iterate over the cows from left to right.
• For each breed, store the position of the last cow of that breed.o Use an array, because 1000000 is small enough.o Could also use a map.
Runtime: O(N)
Silver/Gold: The Cow Run
• N (1 <= N <= 1000) cows on a number line.o Each has a coordinate P (|P| <= 500000)o Each minute, a rampaging cow causes one dollar
worth of damage.
• Farmer John can walk over to a cow and calm it.o He starts at the origin and walks 1 unit per minute.
• Farmer John wants to minimize the total damage. Compute the minimum.
• Sample: N = 4
• Positions: -2, -12, 3, 7
• Output: 50 (visit -2, then 3, then 7, then -12)
Silver/Gold: The Cow RunSolution
• Separate the cows into two arrays: positive / negative coordinates.
• Sort each. Then "merge" the two arrays, visiting the next positive or next negative cow each step. After N steps, all cows will be calmed.
• DP state: #negative cows, #positive cows, and a flag to represent FJ's locationo If flag is 1, FJ is on the positive side.o Else, FJ is on the negative side.
• This is O(N^2).
Gold: Necklace
• Bessie has string A of length N (N <= 10000) and string B of length M (M <= 1000)
• Bessie wants to remove some characters from A so that B is not a substring of A.
• Find the minimum number of characters to remove.
• Sample: A = ababaa, B = aba
• Output: 1. (remove an a to make abbaa)
Gold: NecklaceSolution
• DP State: Position in A, Position in B
• To transition in amortized O(1), use a KMP string matching algorithm failure function table.
• Each step, you can either keep the character in A, or delete it. o If you delete it, Position in B doesn't change. o If you keep it, you compute the new position in B
using the table.
• O(NM)states.
POTW
• You have N nodes, M bidirectional edges.
• How many groups of 3 nodes are all connected to each other or all not connected?
• Input Format:o Line 1: N, Mo Line 2...M+1: a, b: edge from a to b (1 <= a,b <= N)
• Sample: N = 6, M = 3
• Edges: 1 2, 1 3, 2 3
• Output: 2 (123 are all connected, 456 are all not)
• Constraints: 1 <= N,M <= 100000o Partial Credit if it passes for small cases.