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中出现的最右的位置 ...
随机推荐
- 「CF803C」 Maximal GCD
题目链接 戳我 \(Solution\) 令\(gcd\)为\(x\),那么我们将整个序列\(/x\),则序列的和就变成了\(\frac{n}{x}\),所以\(x\)必定为\(n\)的约数所以现在就 ...
- iPhone/iPad调整事件递交
UIKit 为应用程序提供了编程手段来简化事件处理或者完全关闭事件流.下面的列表总结了这些方法: 关闭触摸事件的递交. 缺省情况下,视图接收触摸事件,但是你可以设置它的userInteractionE ...
- Retrofit 使用简介
一,简介 Retrofit 是目前使用广泛的 Http Client 框架,它适用于 Android 和 Java. 但需要注意的是,Retrofit 本身并不是一个网络请求框架,而是一个网络请求框架 ...
- uniapp的微信小程序,获取授权,获取中文街道地理位置
w问题描述:在微信小程序模拟器上运行获取坐标时 获取不到信息,原因是 没有调起默认地理位置: 解决办法:或者在manifest.json的源码视图中配置:配置appid和地理位置 默认弹起获取地理位置 ...
- Ngrinder脚本开发各细节锦集(groovy)
Ngrinder脚本开发各细节锦集(groovy) 1.生成随机字符串(import org.apache.commons.lang.RandomStringUtils) 数字:RandomStrin ...
- PHP AJAX 返回JSON 数据
例子:利用AJAX返回JSON数据,间接访问数据库,查出Nation 表,并用下拉列表显示 造一个外部下拉列表框 </select> JQurey代码 $(document).ready( ...
- Java快排
package quickSort; /** * 快速排序 * @author root * */ public class QuickSort { static int[] data = {0,2, ...
- APT软件包管理-在线安装
APT (Advanced Packaging Tool高级软件包工具) 是一个强大的包管理系统,而那些图形化程序如 添加/删除 应用程序 都是建立 在它的基础之上的.有了dpkg后,Debian再次 ...
- Ubuntu为mysql的root用户密码问题
1.root用户免密码登录mysql Ubuntu装完mysql时,root用户可以免密登录,如果设置的root用户密码忘记了,想要使用root用户免密登录,修改在配置文件的[mysqld]节点下添加 ...
- Matlab中psf2otf()函数在opencv中的实现
在Matlab中有个psf2otf()函数,可以将小尺寸的点扩散函数,扩大尺寸,并作二维傅里叶变换,opencv中没有这个函数,所以编了这么个函数: /************************ ...