传送门

考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$

当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了

发现对于所有与 $i \mod 3$ 相同的位置的字符和 $i$ 位置的字符是一样的

所有 $j \mod 3 = (i+1) \mod 3$位置的字符也都是一样的并且是可以确定的

所有 $j \mod 3 = (i+2) \mod 3$位置的字符也都是一样的并且是确定的

维护 $cnt[0/1/2][0/1/2]$ 表示当前子段所有位置 $\mod 3$ 意义下为 $0/1/2$ ,全部变成 $0/1/2$ (分别对应 $R,G,B$)的代价

那么答案可以很快计算

动态维护 $cnt$ 即可,具体看代码,很简单

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=4e5+,INF=1e9;
int Q,n,m,cnt[][],ans;
char s[N];
inline void up(int i,int v)
{
if(s[i]=='R') cnt[i%][]+=v,cnt[i%][]+=v;
if(s[i]=='G') cnt[i%][]+=v,cnt[i%][]+=v;
if(s[i]=='B') cnt[i%][]+=v,cnt[i%][]+=v;
}
int main()
{
Q=read();
while(Q--)
{
n=read(),m=read();
scanf("%s",s+); ans=INF;
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++)
{
up(i,); if(i>m) up(i-m,-);
if(i<m) continue;
for(int j=;j<;j++)
{
int t=;
for(int k=;k<;k++) t+=cnt[k][(j+k)%];
ans=min(ans,t);
}
}
printf("%d\n",ans);
}
return ;
}

Codeforces 1196D2. RGB Substring (hard version)的更多相关文章

  1. Codeforces 1196D2 RGB Substring (Hard version) 题解

    题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...

  2. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  3. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

  4. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  5. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”. ...

  6. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

  7. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  8. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  9. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

随机推荐

  1. [CSP-S模拟测试]:序列(二分答案+树状数组)

    题目传送门(内部题98) 输入格式 第一行一个整数$n$,第二行$n$个整数$a_1\sim a_n$,第三行$n$个整数$b_1\sim b_n$. 输出格式 一行一个整数表示$\max(r-l+1 ...

  2. Spring boot 读取resource目录下的文件

    背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource ...

  3. gcd表(欧几里得定理)

    题目:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=797 gcd表 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ...

  4. BeanFactory和ApplicationContext的区别+部分Spring的使用

    BeanFactory和ApplicationContext的区别 ApplicationContext 方式加载:创建容器的同时 容器初始化,容器所有的bean创建完毕   Spring容器中获取一 ...

  5. 2018-2019-20175329 实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告

    2018-2019-20175329 实验三敏捷开发与XP实践<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...

  6. Linux Ubuntu 用c++11编译

    加上: -std=c++ 例如: g++ test.

  7. Windows10 修改键位映射

    https://blog.csdn.net/lhdalhd1996/article/details/90741092 1.为什么要修改键位我的笔记本键盘上下键是这样的: 很想捶死产品经理,你是从来不用 ...

  8. windows10下如何完全卸载串口驱动?

    答: 所需步骤如下: 1. 以管理员身份启动cmd 2. 在cmd中输入两行命令来启动设备管理器 3. 设置view->Show hidden devices 4. 在Ports (COM &a ...

  9. TortoiseGit的安装

    运行TortoiseGit-1.7.7.0-32bit.msi, 如下图 点击Next, 下一步 点击Next, 下一步 选择ssh客户端, 使用默认, 点击Next, 下一步 选择安装路径, 点击N ...

  10. debian中安装pip

    sudo apt upate sudo apt-get install python3-pip