Codeforces D. Omkar and Bed Wars

D. Omkar and Bed Wars

Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are n players arranged in a circle, so that for all j such that 2≤j≤n, player j−1 is to the left of the player j, and player j is to the right of player j−1. Additionally, player n is to the left of player 1, and player 1 is to the right of player n.

Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either 0, 1, or 2 other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly 1 other player, then they should logically attack that player in response. If instead a player is being attacked by 0 or 2 other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.

Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the n players in the game to make them instead attack another player — i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left.

Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The descriptions of the test cases follows.

The first line of each test case contains one integer n (3≤n≤2⋅105) — the amount of players (and therefore beds) in this game of Bed Wars.

The second line of each test case contains a string s of length n. The j-th character of s is equal to L if the j-th player is attacking the player to their left, and R if the j-th player is attacking the player to their right.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy.

It can be proven that it is always possible for Omkar to achieve this under the given constraints.

Example
inputCopy
5
4
RLRL
6
LRRRRL
8
RLLRRRLL
12
LLLLRRLRRRLL
5
RRRRR
outputCopy
0
1
1
3
2
Note
In the first test case, players 1 and 2 are attacking each other, and players 3 and 4 are attacking each other. Each player is being attacked by exactly 1 other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer 0.

In the second test case, not every player acts logically: for example, player 3 is attacked only by player 2, but doesn’t attack him in response. Omkar can talk to player 3 to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer 1.

题意:
有一个游戏,把大家围成一个圈,每一个人都可以攻击相邻的人,这样会造成人受到的攻击数为0,1,2,当只受到一个人的攻击时,此时你必须要反击这个人才算合理,如果没有反击的话,你需要进行纠正,问最少需要纠正多少次。

解题思路:
分为两种情况
①一种是形如RRRRR… 所有人完全一样,则直接输出1+(n-1)/3即可。
②另一种是由多个段组成,例如RLRLRRRLLRRR,此时我们可以将连续每段的长度存起来,即1 1 1 1 3 2 3 ,由于是一个环,因此首尾相同的话需要合并,我这里是合并到尾处,所以最终为 0 1 1 1 3 2 4。仔细分析会发现每连续的3个就需要变一下,对每一段,分别除3再相加即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int t,n,a[200005];
string str;
vector<int> v;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        v.clear();
        scanf("%d",&n);
        cin>>str;
        int cnt=1;
        for(int i=1;i<n;i++)
        {
            if(str[i]==str[i-1]) 
				cnt++;
            else
            {
                v.push_back(cnt); //将所有连续相同字符的子串长度存起来 例如如果是RRRLLRRR 则存取结果为 3 2 3 
                cnt=1; 
            }   
        }
        v.push_back(cnt);
        if(v.size()>1&&str[0]==str[n-1]) //如果发现最后一个字符与第一个字符相同,由于是循环,因此将他们合并 
        {
            v[v.size()-1]=v[0]+v.back(); //合并后的结果为 0 2 6 
            v[0]=0;
        }
        
        if(v.size()==1) //如果只是RRRR...或者LLLL...类型 ,完全相同的一段,则直接输出 
            printf("%d\n",1+(v[0]-1)/3);
        else //反之就是多段混合在一起的,这时利用vector中存取的各段长度,分别除3相加即可 
        {
            int ans=0;
            for(int i=0;i<v.size();i++) 
				ans+=v[i]/3;
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

henulmh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值