Codeforces 1196D2. RGB Substring (hard version)
考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $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)的更多相关文章
- Codeforces 1196D2 RGB Substring (Hard version) 题解
题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- 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 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”. ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)
传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...
- 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 ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
随机推荐
- python3精品解析运算符
算数运算符 +:两个对象相加 -:得到负数或者,或者一个数减去另一个数 *:两个数相乘或者是返回一个被重复若干次的字符串 /:5/2等于2.1 5//2=2(/有余数,//取整) %:取模(5%2=1 ...
- nmap脚本nse的使用
nmap脚本(nse)使用总结 0x01 nmap按脚本分类扫描 nmap脚本主要分为以下几类,在扫描时可根据需要设置--script=类别这种方式进行比较笼统的扫描: auth: 负责处理鉴权证书( ...
- 08.青蛙跳台阶 Java
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 思路 暴力枚举(自顶向下递归): 若台阶数小于等于0,返回0: 若台阶 ...
- Linux 竞态条件和临界区
1. 临界区和竞态条件: 临界区:访问和操作共享数据的代码段: 竞态条件:当有多个线程同时进入临界区时,执行结果取决于线程的执行顺序: 如下述代码,当多个线程同时调用func函数,对共享数据sum进行 ...
- word 之 插入删除空行
好久没有写程序了.有些手生: 在用C#对word进行直接开发操作过程中,为了文档的美观,我们会插入或删除空行. 1.插入空行的代码很简单. Selection 类型的TypeParagraph()函数 ...
- Python 图形界面元素
from tkinter import * import os def button_click1(): try: filePath = r'D:\CloudMusic' os.system(&quo ...
- python--nolocal
Compare this, without using nonlocal: x = 0def outer(): x = 1 def inner(): x = 2 print("inner:& ...
- SQL Server 数据库设计、命名、编码规范
https://blog.csdn.net/songguozhi/article/details/5858159 SQL Server 数据库设计.命名.编码规范
- Vue -3:单文件组件
在很多 Vue 项目中,我们使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: '#container '}) 在每个页面内指定一个容器元素. 这种方式在很多中小规 ...
- Shutdown 源码阅读
Shutdown /** * 虚拟机关闭步骤 * @since 1.3 */ class Shutdown { /* 关闭状态 */ private static final int RUNNING ...