当前位置: 代码迷 >> 综合 >> Codeforces Global Round 10 D. Omkar and Bed Wars(DP)
  详细解决方案

Codeforces Global Round 10 D. Omkar and Bed Wars(DP)

热度:61   发布时间:2024-02-21 01:36:51.0

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

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 ? 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 ? (1≤?≤104). The descriptions of the test cases follows.

The first line of each test case contains one integer ? (3≤?≤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 ? of length ?. The ?-th character of ? is equal to L if the ?-th player is attacking the player to their left, and R if the ?-th player is attacking the player to their right.

It is guaranteed that the sum of ? 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.

题意:
环形序列,每个人可以向左边攻击也可以向右边攻击。如果一个人没有被攻击或者被两边,他可以随便攻击哪个。否则必须向攻击自己的方向攻击。
求最少改变多少个人的攻击方向使得序列符合上述规则。

思路:
只可能有4种攻击方式(其他攻击方式由此组成,且互相不干扰)
RL
RLL, RRL
RRLL

那么可以定义dp[i]dp[i]dp[i]为前iii个操作最少需要修改多少个。不过需要注意的是这是环形序列,所以要枚举起点,并且最多只要枚举4个就好了(首部1个,尾部3个组成长度为4的攻击单位)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <unordered_map>
#include <bitset>using namespace std;typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 998244353;char s[maxn];
int n,dp[maxn];void change() {
    char ch = s[1];for(int i = 1;i < n;i++) {
    s[i] = s[i + 1];}s[n] = ch;
}void DP() {
    for(int i = 1;i <= n;i++) {
    dp[i] = 1e9;}dp[0] = 0;for(int i = 2;i <= n;i++) {
    dp[i] = min(dp[i - 2] + (s[i] != 'L') + (s[i - 1] != 'R'),dp[i]);if(i >= 3) dp[i] = min(dp[i - 3] + (s[i] != 'L') + (s[i - 2] != 'R'),dp[i]);if(i >= 4) dp[i] = min(dp[i - 4] + (s[i] != 'L') + (s[i - 1] != 'L' ) + (s[i - 2] != 'R') + (s[i - 3] != 'R'),dp[i]);}
}int main() {
    int T;scanf("%d",&T);while(T--) {
    scanf("%d",&n);scanf("%s",s + 1);int ans = 1e9;for(int i = 1;i <= 4;i++) {
    DP();change();ans = min(ans,dp[n]);}printf("%d\n",ans);}return 0;
}
  相关解决方案