Alex_tz307

USACO 2017 January Contest, Gold Problem 2. Hoof, Paper, Scissors

Mar 14th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("hps.in");
  7. ofstream fout("hps.out");
  8.  
  9. /// H = 0, P = 1, S = 2
  10. /// 0 > 2
  11. /// 2 > 1
  12. /// 1 > 0
  13.  
  14. const int NMAX = 1e5 + 5;
  15. const int KMAX = 22;
  16. int N, K, dp[NMAX][KMAX][3], ans;
  17. string moves = "";
  18.  
  19. /// dp[i][switches][play] - numarul maxim de jocuri pe care le poate castiga Bessie
  20. ///                         daca la pasul i a facut switches schimbari de stare
  21. ///                         si joaca "miscarea" play
  22.  
  23. int num(const char &ch) {
  24.     if(ch == 'H')
  25.         return 0;
  26.     if(ch == 'P')
  27.         return 1;
  28.     return 2;
  29. }
  30.  
  31. bool wins(const int &x, const int &y) {
  32.     return (x == 0 && y == 2) || (x == 2 && y == 1) || (x == 1 && y == 0);
  33. }
  34.  
  35. void max_self(int &a, int b) {
  36.     a = max(a, b);
  37. }
  38.  
  39. int main() {
  40.     fin >> N >> K;
  41.     fin.get();
  42.     for(int i = 0; i < N; ++i, fin.get())
  43.         moves += fin.get();
  44.     for(int play = 0; play < 3; ++play)
  45.         dp[0][0][play] = wins(play, num(moves[0]));
  46.     for(int i = 1; i < N; ++i)
  47.         for(int switches = 0; switches <= K; ++switches)
  48.             for(int prv_play = 0; prv_play < 3; ++prv_play)
  49.                 for(int curr_play = 0; curr_play < 3; ++curr_play) {
  50.                     int new_switches = switches + (curr_play != prv_play);
  51.                     if(new_switches > K)
  52.                         continue;
  53.                     max_self(dp[i][new_switches][curr_play], dp[i - 1][switches][prv_play] + wins(curr_play, num(moves[i])));
  54.                 }
  55.     for(int k = 0; k <= K; ++k)
  56.         for(int play = 0; play < 3; ++play)
  57.             max_self(ans, dp[N - 1][k][play]);
  58.     fout << ans << '\n';
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment