时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:
·The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)
·Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):
→R points for winning with Rock;
→S points for winning with Scissors;
→P points for winning with Paper.
·However, in the i-th round, the player cannot use the hand he/she used in the (i−K)-th round. (In the first K rounds, the player can use any hand.)
Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.
The information Takahashi obtained is given as a string T. If the i-th character of T (1≤i≤N)
is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.
What is the maximum total score earned in the game by adequately choosing the hand to play in each round?
Notes
In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.
·If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;
·if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;
·if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;
·if both players play the same hand, it is a draw.
Constraints
·2≤N≤10^5
·1≤K≤N−1
·1≤R,S,P≤10^4
·N,K,R,S, and P are all integers.
·|T|=N
·T consists of r, p, and s.
输入
Input is given from Standard Input in the following format:
N K
R S P
T
输出
Print the maximum total score earned in the game.
样例输入 Copy
【样例1】
5 2
8 7 6
rsrpr
【样例2】
7 1
100 10 1
ssssppr
【样例3】
30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr
样例输出 Copy
【样例1】
27
【样例2】
211
【样例3】
4996
提示
样例1解释
The machine will play {Rock, Scissors, Rock, Paper, Rock}.
We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points. We cannot earn more points, so the answer is 27.
题目大意是求和机器玩游戏得到的最大分数。
注意要分段讨论,前k回合hand任意,利用贪心思想直接找能够赢的情况。
易错点来了!!!**中间部分(不包括后k回合)不仅要考虑往前k个位置的hand,还要考虑往后k个位置的hand。**假设前面的情况能够保证是最优解,由于题目要求第i个位置和第i-k位置的hand不能重复,在第i个回合上一定能排除一种hand不能出,那么再结合第i+k个回合的hand,在确保第i+k个回合满足最优解的情况下确定第i个回合的hand。对于后k个回合每个回合i,则直接根据第i-k个回合确定能够赢的情况。具体思路见代码。
#include<stdio.h>
typedef long long int lli;//long long int防止结果溢出
char t[100005];//代表机器人的所有手势
char t1[100005];//代表玩家的手势,用于中间过程的判断
int main()
{
int n;
int k;
lli r;
lli s;
lli p;
int i;
lli sum=0;
scanf("%d %d",&n,&k);
scanf("%lld %lld %lld",&r,&s,&p);
scanf("%s",t+1);
for(i=1;i<=n;i++)
{
if(i<=k)//直接找最优解
{
if(t[i]=='r')
{
t1[i]='p';
sum+=p;
}
else if(t[i]=='s')
{
t1[i]='r';
sum+=r;
}
else if(t[i]=='p')
{
t1[i]='s';
sum+=s;
}
}
else if(i<=n-k)//瞻前顾后
{
if(t[i]=='r')
{
if(t1[i-k]!='p')//满足最优解且不重复
{
t1[i]='p';
sum+=p;
}
else//重复
{
if(t[i+k]=='s')t1[i]='s';//保证第i+k回合的hand是最优解
else if(t[i+k]=='p')t1[i]='r';
else if(t[i+k]=='r')t1[i]='s';
}
}
else if(t[i]=='s')
{
if(t1[i-k]!='r')
{
t1[i]='r';
sum+=r;
}
else
{
if(t[i+k]=='s')t1[i]='s';
else if(t[i+k]=='p')t1[i]='p';
else if(t[i+k]=='r')t1[i]='s';
}
}
else if(t[i]=='p')
{
if(t1[i-k]!='s')
{
t1[i]='s';
sum+=s;
}
else
{
if(t[i+k]=='s')t1[i]='p';
else if(t[i+k]=='p')t1[i]='r';
else if(t[i+k]=='r')t1[i]='r';
}
}
}
else//直接根据前面的数找最优解,不再更新t1[]的值
{
if(t[i]=='r')
{
if(t1[i-k]!='p')sum+=p;
}
else if(t[i]=='s')
{
if(t1[i-k]!='r')sum+=r;
}
else if(t[i]=='p')
{
if(t1[i-k]!='s')sum+=s;
}
}
}
printf("%lld",sum);
return 0;
}
/**************************************************************
Language: C++
Result: 正确
Time:5 ms
Memory:1316 kb
****************************************************************/