BZOJ 4760 dp

来源:互联网 发布:宜家八斗柜 知乎 编辑:程序博客网 时间:2024/05/29 04:17

Hoof, Paper, Scissors

Time Limit: 10 Sec  Memory Limit: 128 MB
[Submit][Status][Discuss]

Description

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game th
ey call "Hoof, Paper, Scissors"
你可能玩过石头剪刀布这个游戏,奶牛们也喜欢玩类似的游戏,叫做“蹄子剪刀布”
The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each-other. They both count t
o 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 b
eats 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 sec
ond 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 N games of "Hoof, Paper, Scissors" (1≤N
≤100,000). Bessie, being an expert at the game, can predict each of FJ's gestures before he makes i
t. Unfortunately, Bessie, being a cow, is also very lazy. As a result, she tends to play the same ge
sture multiple times in a row. In fact, she is only willing to switch gestures at most KK times over
 the entire set of games (0≤K≤20). For example, if K=2, she might play "hoof" for the first few ga
mes, then switch to "paper" for a while, then finish the remaining games playing "hoof".
现在FJ想要和他的最机智的奶牛Bessie玩蹄子剪刀布(我也不知道FJ为什么有蹄子),一共进行了N轮(N<=1e5),B
essie,作为一个奶牛,非常的怠惰,无论她出什么,都喜欢连续的出,最多变化K次(K<=20),也就是说,对于她
所出的,记为序列f(i),记sum=有多少个i满足f(i)!=f(i-1)(i>1),而她的sum一定不会超过k
Given the sequence of gestures FJ will be playing, please determine the maximum number of games that
 Bessiecan win.
现在FJ已经给出了他出的东西,你要告诉Bessie,在不确定她出的东西的情况下,她最多能赢多少次

Input

The first line of the input file contains N and K.
输入数据第一行为N,K
The remaining N lines contains FJ's gestures, each either H, P, or S
接下来N行表示FJ所出的东西,H表示hoof,P表示paper,S表示Scissors

Output

Print the maximum number of games Bessie can win, given that she can only change gestures at most KK times.
输出在变化不超过K次的前提下,最多能赢多少次

Sample Input

5 1
P
P
H
P
S

Sample Output

4


题解:定义dp[i][j][k]为前i场转换j次当前出的是k能赢的场数

状态转移方程就确定了


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<cmath>using namespace std;typedef long long ll;int a[100005],dp[100005][25][4],sum[100005][4];int judge(int a,int b){if(a==1)return b==2;if(a==2)return b==3;return b==1;}int main(){char c[3];int n,k,i,j;scanf("%d%d",&n,&k);for(i=1;i<=n;i++){scanf("%s",c);if(c[0]=='P')a[i]=1;else if(c[0]=='H')a[i]=2;else a[i]=3;sum[i][1]=sum[i-1][1];sum[i][2]=sum[i-1][2];sum[i][3]=sum[i-1][3];sum[i][a[i]]++;dp[i][0][1]=dp[i-1][0][1]+judge(1,a[i]);dp[i][0][2]=dp[i-1][0][2]+judge(2,a[i]);dp[i][0][3]=dp[i-1][0][3]+judge(3,a[i]);}for(i=1;i<=n;i++){for(j=1;j<=k;j++){dp[i][j][1]=max(dp[i-1][j][1],max(dp[i-1][j-1][2],dp[i-1][j-1][3]))+judge(1,a[i]);dp[i][j][2]=max(dp[i-1][j][2],max(dp[i-1][j-1][1],dp[i-1][j-1][3]))+judge(2,a[i]);dp[i][j][3]=max(dp[i-1][j][3],max(dp[i-1][j-1][2],dp[i-1][j-1][1]))+judge(3,a[i]);}}cout<<max(dp[n][k][1],max(dp[n][k][2],dp[n][k][3]))<<endl;return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电视无信号出雪花怎么办 电视打开都是雪花没有电视台怎么办 电视视频1无信号怎么办 新买的电视无信号怎么办 卫星有信号没有视频怎么办 户户通没有信号怎么办视频 雪花泥粘衣服上怎么办 遗产按份额处分判决后怎么办 宝马后驱车下雪天怎么办 宝马后驱车路滑怎么办 车子陷入泥地里怎么办 深圳居住证签注过期了怎么办 手机不能播放视频乱码了怎么办 被加密的视频无法观看怎么办 苹果5忘记id密码怎么办 七个月宝宝脾胃不好怎么办 八个月宝宝脾虚怎么办 七个月宝宝脾胃虚怎么办 海岛奇兵点错了怎么办 螳螂的脚断了怎么办 海岛奇兵打不过玩家怎么办 海岛奇兵资源满了怎么办 海岛奇兵杯越来越多打不玩家怎么办 海岛奇兵控杯技巧 杯数太高怎么办 海岛奇兵发现求救信号怎么办 海岛奇兵被打了怎么办 小鱼翅卡喉咙了怎么办 鱼翅卡在喉咙里怎么办 斗鱼身份证被使用怎么办 做的鱼丸太腥了怎么办 做鱼丸太稀了怎么办 斗鱼手机号换了怎么办 斗鱼直播掉帧怎么办 手机一直卡顿点不动怎么办呢 斗鱼直播分值底怎么办 斗鱼6000鱼丸怎么办卡 斗鱼直播没人看怎么办 淘宝直播间没人气怎么办 挂水了还是有热度怎么办 陌陌工会不结算工资怎么办 滴滴给了差评怎么办