传送门

考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $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. vue绑定样式

    使用三目运算符绑定样式 本来以为使用vue模版写法,在绑定单个样式,也就是一个class类名的时候可以直接书写,就像这样 <div id="app"> <div ...

  2. 打开下载CA root 证书的链接失败

    下载CA root 证书 http://adip/certsrv server error or refuse to connect 这是由于AD上没有安装Active Directory Certi ...

  3. Mybatis不使用Spring框架(Druid)

    1.Mysql database CREATE TABLE mockrecord (id INT(10) unsigned PRIMARY KEY NOT NULL COMMENT 'mock记录编号 ...

  4. centos7配置外网

    1设置网络连接中的NAT网络配置 2虚拟机的网络设置选择NAT连接设置如下,子网IP可通过ipconfig查看本地VMnet8,如我本地VMnet8 ip为:192.168.198.0 3 3.开启虚 ...

  5. Copy-On-Write in Swift

    Premature optimisation is the root of all evil. But, there are moments where we need to optimise our ...

  6. javascript之for-in语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. C++异常实现机制

    1.C函数的调用和返回 要理解C++异常机制实现之前,首先要了解一个函数的调用和返回机制,这里面就要涉及到ESP和EBP寄存器.我们先看一下函数调用和返回的流程. 下面是按调用约定__stdcall ...

  8. 阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架

    service能供成功的调用到service对象就算是整合成功 如果能把生成的代理对象也存大IOC的容器中.那么ServiceImpl就可以拿到这个对象 做一个注入,然后就可以调用代理对象的查询数据库 ...

  9. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_1 搭建环境

    创建项目 使用骨架,创建webapp 为了创建项目更快速maven设置 archetypeCatalog internal 修改编译的版本 从昨天的课程内复制 相关的坐标.上面是版本锁定. 复制前端的 ...

  10. IPTV系统的VOD与TV业务性能测试

    IPTV的未来发展正在成为业界的焦点话题.据市场研究公司MRG的统计,全球IPTV用户将由2004年的200万增加至2010年的2000万,预计全球IPTV市场2005-2010年的复合增长率为102 ...