USACO Gold 2017 January - Hoof, Paper, Scissors
Authors: Melody Yu, Neo Wang, Brad Ma
Video Solution
Note: The video solution might not be the same as other solutions. Code in C++.
Solution
The main observation for this problem is that we only need to keep track of the number of games played , the number of times switched so far , and the current gesture in order to determine the largest number of previous games won for any game .
For every move, either Bessie can either switch or stay on her current gesture. If she changes her gesture, then the next game will have used gestures, which corresponds to the state . We can simulate this for all 3 gestures. Then, we just increment if Bessie wins at game with gesture .
Note that you can just compare the current gesture to H, P, S
because there is
always exactly one way to win.
Time Complexity:
C++
Implementation
Code Snippet: C++ Short Template (Click to expand)const int MX = 1e5 + 5;int dp[MX][25][3]; // dp[i][j][k] is the largest number of games she wins at// games i with switches j with current item kint moves[MX]; // 0 == H 1 == P 2 == Sint main() {setIO("hps");
Java
import java.io.*;import java.util.*;public class HoofPaperScissors {static int[] moves;public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("hps.in"));StringTokenizer inputLine = new StringTokenizer(br.readLine());int numGames = Integer.parseInt(inputLine.nextToken());
Python
Warning!
Due to Python's constant factor, the below solution TLEs on a couple test cases.
with open("hps.in") as read:n, k = map(int, read.readline().strip().split())moves = [0] * nfor i in range(n):a = read.readline().strip()if a == "H":moves[i] = 0elif a == "P":moves[i] = 1else:
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!