• 1196D2


    1196D2 - RGB Substring (hard version)


    #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(ll i=a;i<=b;i++) #define dec(i,a,b) for(ll i=a;i>=b;i--) #define forn(i, n) for(ll i = 0; i < int(n); i++) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod = 1e7 + 7; inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } inline ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } void exgcd(ll A, ll B, ll& x, ll& y) { if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0; } bool prime(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } inline int qpow(int x, ll n) { int r = 1; while (n > 0) { if (n & 1) r = 1ll * r * x % mod; n >>= 1; x = 1ll * x * x % mod; } return r; } /**********************************************************/ int len , n; int solve(string& s,string& t) { vector<int> v(len); int cnt =0; forn(i,len) { if(s[i]!=t[i]) v[i]=1; } int i=0,j=0; while(j<n) { if(v[j]==1) cnt++; j++; } int ans=inf; while(j<len) { ans=min(ans,cnt); if(j<len) cnt+=-v[i]+v[j]; i++,j++; } ans=min(ans,cnt); return ans; } int main(){ int T; cin>>T; while(T--) { cin>>len>>n; string s; cin>>s; string t; for(int i=0;i<len;i++) { if(i%3==0) t+='R'; else if (i%3==1) t+='G'; else t+='B'; } int ans=inf; ans=min(ans,solve(s,t)); rep(i,0,1) { t.erase(0,1); if((len+i)%3==0) t+='R'; else if ((len+i)%3==1) t+='G'; else t+='B'; ans=min(ans,solve(s,t)); } cout<<ans<<endl; } }

     尺取的时候长度应该是n+1

  • 相关阅读:
    Codeforces 876C Classroom Watch:枚举
    Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】
    BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树
    BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏:dfs【无根树 节点分枝子树大小】
    markdown常用数学符号小结
    BZOJ3211花神游历各国-线段树&树状数组-(HDU4027同类型)
    一维二维树状数组写法总结
    hdu4352-XHXJ's LIS状压DP+数位DP
    常用Git命令以及出现的状况ing
    bitset简单用法
  • 原文地址:https://www.cnblogs.com/dealer/p/13370727.html
Copyright © 2020-2023  润新知