P8266 题解
Register_int · · 题解
今天模拟赛刚好考到了,就写个题解。
首先,将每两头相邻的奶牛捆到一起,一次翻转可以先看成将每一个组合逐个翻转。之后,新建序列
记得特判第一段区间是否为前缀。
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
int n, l, r, f, ans;
char s[MAXN];
int a[MAXN << 1];
int main() {
scanf("%d%s", &n, s);
for (int i = 0; i < n; i += 2) {
if (s[i] == 'G' && s[i + 1] == 'H') a[i >> 1] = 0;
else if (s[i] == 'H' && s[i + 1] == 'G') a[i >> 1] = 1;
else a[i >> 1] = 2;
}
n >>= 1;
while (r < n && a[r] != 1) f |= !a[r++]; ans += f, l = ++r;
while (l < n) { f = 0; while (r < n && a[r] != 1) f |= !a[r++]; ans += f << 1, l = ++r; }
printf("%d", ans);
}