简单的情况不用前缀和,直接暴力就好,数据范围大的D2,就用前缀和数组存不满足三种情况的数,最后减一下取小就可以。
1 #include<bits/stdc++.h> 2 #define mem(a) memset(a,0,sizeof(a)) 3 #define ll long long 4 #define ld long double 5 #define ull unsigned long long 6 #define mp make_pair 7 #define pb push_back 8 #define inf 0x3f3f3f3f 9 using namespace std; 10 const int N=2e5+5; 11 const int M=100+5; 12 int n,t,k,x[N],y[N],z[N]; 13 string s; 14 char ss[3]={‘R‘,‘G‘,‘B‘}; 15 int main() 16 { 17 cin>>t; 18 while(t--) 19 { 20 cin>>n>>k; 21 cin>>s; 22 int ans=n+5; 23 x[0]=y[0]=z[0]=0; 24 for(int i=0;i<n;i++) 25 { 26 if(s[i]!=ss[i%3]) x[i+1]=x[i]+1; 27 else x[i+1]=x[i]; 28 if(s[i]!=ss[(i+1)%3]) y[i+1]=y[i]+1; 29 else y[i+1]=y[i]; 30 if(s[i]!=ss[(i+2)%3]) z[i+1]=z[i]+1; 31 else z[i+1]=z[i]; 32 } 33 for(int i=0;i+k<=n;i++) 34 { 35 ans=min(min(ans,x[i+k]-x[i]),min(y[i+k]-y[i],z[i+k]-z[i])); 36 } 37 cout<<ans<<endl; 38 } 39 }
(? •_•)?
原文地址:https://www.cnblogs.com/XXrll/p/11264399.html
时间: 2024-10-27 20:37:50