USACO17JAN Hoof Paper Scissor 蹄子,剪刀,布

来源:互联网 发布:天龙八部知乎 编辑:程序博客网 时间:2024/04/28 07:53

题目描述

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors".

The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each-other. They both count to three and then each simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (since a hoof can smash a pair of scissors), scissors beats paper (since scissors can cut paper), and paper beats hoof (since the hoof can get a papercut). For example, if the first cow makes a "hoof" gesture and the second a "paper" gesture, then the second cow wins. Of course, it is also possible to tie, if both cows make the same gesture.

Farmer John wants to play against his prize cow, Bessie, at  games of "Hoof, Paper, Scissors" (). Bessie, being an expert at the game, can predict each of FJ's gestures before he makes it. Unfortunately, Bessie, being a cow, is also very lazy. As a result, she tends to play the same gesture multiple times in a row. In fact, she is only willing to switch gestures at most  times over the entire set of games (). For example, if , she might play "hoof" for the first few games, then switch to "paper" for a while, then finish the remaining games playing "hoof".

Given the sequence of gestures FJ will be playing, please determine the maximum number of games that Bessie can win.

你可能听说过“石头,剪刀,布”的游戏。FJ的牛喜欢玩一个类似的游戏,它们称之为“蹄子,剪刀,布”(“蹄子”就是“石头”)。

游戏规则很简单:比赛双方同时数到3,然后同时出一个手势,代表“蹄子”“剪刀”或“布”。“蹄子”胜“剪刀”,“剪刀”胜“布”,“布”胜“蹄子”。举个例子,第一头牛出“蹄子”,第二头牛出“布”,则第二头牛胜利。当然,也可以“平局”(如果两头牛手势相同的话)。

FJ想对阵自己获奖的牛,贝西。贝西作为一个专家,能够预测FJ的手势。不幸的是,贝西作为一头牛,也十分的懒惰。事实上,她只愿意变换固定次数的手势来完成游戏。例如,她可能只想变1次,则他可能出“蹄子”几次,剩下的都出“布”。

鉴于贝西预测FJ会出的手势,以及她想变的次数,求出她最多能赢多少场

输入输出格式

输入格式:

The first line of the input file contains  and .

The remaining  lines contains FJ's gestures, each either H, P, or S.

输出格式:

Print the maximum number of games Bessie can win, given that she can only change gestures at most  times.

输入输出样例

输入样例#1:
5 1PPHPS
输出样例#1:
4

                                                                         










        正常做法:f[i][j][k]表示前i次,分j段,最后一次出的是k(k=0,1,2)时最多赢几次,可以O(nk)解决,转移时看最近一次有没有新分一段即可.

        智障做法:我们同样定义f[i][j][k]表示前i次,分j段,最后一次出的是k时最多赢几次,但转移的时候考虑最近的一段,也就是枚举最近的一段是从哪里开始的,那么这就变成了1D1D动态规划,打表观察到决策单调性,可以大力写一个决策单调性O(nlognk)解决,强行多一个log也可以过.

        但其实我觉得吧,决策单调性也挺好写的。。。

#include<iostream>  #include<stdio.h>  #include<algorithm>  using namespace std;  const int maxn=100001;  int array[maxn],dp[maxn][21][4];  int ans;  inline int check(int x,int y){      if(x==1&&y==2) return 1;      else if(x==2&&y==3) return 1;      else if(x==3&&y==1) return 1;      return 0;  }  inline int got_into(int ch1,int ch2,int ch3){      if(ch1>ch2) return ch1>ch3?ch1:ch3;      else return ch2>ch3?ch2:ch3;    }  int main(){  //  freopen("game.in","r",stdin);  //  freopen("game.out","w",stdout);      int n,k;      cin>>n>>k;      for(int i=1;i<=n;i++){          char c;          cin>>c;          if(c=='H') array[i]=1;          if(c=='S') array[i]=2;          if(c=='P') array[i]=3;      }      for (int i=1;i<=n;i++){          dp[i][0][1]=dp[i-1][0][1]+check(1,array[i]);          dp[i][0][2]=dp[i-1][0][2]+check(2,array[i]);          dp[i][0][3]=dp[i-1][0][3] +check(3,array[i]);      }      for(int j=1;j<=k;j++){          for(int i=1;i<=n;i++){              dp[i][j][1] =got_into(dp[i-1][j][1] ,dp[i-1][j-1][2] ,dp[i-1][j-1][3] )+check(1,array[i]);          dp[i][j][2] =got_into(dp[i-1][j][2] ,dp[i-1][j-1][1] ,dp[i-1][j-1][3] )+check(2,array[i]);          dp[i][j][3] =got_into(dp[i-1][j][3] ,dp[i-1][j-1][1] ,dp[i-1][j-1][2] )+check(3,array[i]);          }      }       int ans1=-1,ans2=-1,ans3=-1;      for(int i=1;i<=n;i++){          ans1=max(ans1,dp[n][k][1] );          ans2=max(ans2,dp[n][k][2] );          ans3=max(ans3,dp[n][k][3] );      }      int ans=got_into(ans1,ans2,ans3);      cout<<ans<<endl;      return 0;  }  
嗯哼》》。。。


原创粉丝点击