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中出现的最右的位置 ...
随机推荐
- python中的定时器threading.Timer
由浅入深学SQL Server 2012 --> python开发中用到,定时操作.例如每隔1s执行一次,发现 threading.Timer,这个东西,可以直接用. 其原理为执行函数中置定时 ...
- zeppelin 无法连接一个已有的standalone模式的spark集群
SparkInterpreter.java 这个文件里面读取master的属性有些问题: 原来代码中"master"属性的获取的地方应该是错了.设置和读取这个属性的对象不是同一个 ...
- 如何修改phpstorm的缓存目录
相信使用phpstorm的人们都被缓存目录的大小困扰过.怎么修改到其它地方呢? 1. 找到 idea.properties 文件,配置信息都在此文件中,F:\Program Files\JetBrai ...
- ServiceStatusUtils判断服务是否运行
import android.app.ActivityManager; import android.app.Service; import android.content.Context; impo ...
- 如何在网页中添加 jQuery。
转自:http://www.runoob.com/jquery/jquery-install.html 网页中添加 jQuery 可以通过多种方法在网页中添加 jQuery. 您可以使用以下方法: 从 ...
- py matplotlib 多个figure同时画多个图以及多个图例多个折线图
图例负号乱码的问题 import numpy as np import matplotlib.pyplot as pltimport matplotlibplt.rcParams['axes.un ...
- 堆的ptmalloc机制
ptmalloc下堆的分配和回收 ptmalloc内存分配 1) 获取分配区的锁,为了防止多个线程同时访问同一个分配区,在进行分配之前需要取得分配区域的锁.线程先查看线程私有实例中是否已经存在一个分配 ...
- httpClient模仿Basic Auth
httpclient代码调用如下: @GetMapping("loginTest") public String httpClientWithBasicAuth() { Strin ...
- 3、kubeadm初始化Kubernetes集群
同步时间 # ntpdate time.nist.gov k8s集群组成 k8s部署环境 https://kubernetes.io/docs/setup/independent/create-cl ...
- wmic查询ip
@echo off for /F "usebackq" %%R in (`wmic PATH Win32_NetworkAdapterConfiguration WHERE &qu ...