-
Notifications
You must be signed in to change notification settings - Fork 0
/
D2.cpp
80 lines (75 loc) · 1.72 KB
/
D2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//https://codeforces.com/contest/1196/problem/D2
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const int AC = 3;
const int INF = 1e9 + 5;
int dp[AC][maxn+1] = { { 0 } };
void solve();
bool good(int c, char tt);
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int q; cin >> q;
while (q--)
solve();
}
bool good(int c, char tt)
{
switch(tt)
{
case 'R':
return c == 0;
case 'G':
return c == 1;
case 'B':
return c == 2;
default:
return false;
}
}
void solve()
{
// 0 - R, 1 - G, 2 - B
// c - k % 3
int n, k; cin >> n >> k; string s; cin >> s;
int minres = INF;
for(int i = 0; i < k - 1; ++i)
{
dp[0][i] = 0;
dp[1][i] = 0;
dp[2][i] = 0;
}
int cnt = 0;
int curr;
for(int c = 0; c <= 2; ++c)
{
cnt = 0;
curr = c;
// I want dp[c][k-1], rest is trivial
for(int i = k - 1; i >= 0; --i)
{
if (!good(curr,s[i]))
{
cnt++;
}
--curr;
if (curr == -1) curr = 2;
}
dp[c][k-1] = cnt;
minres = min(dp[c][k-1], minres);
}
for(int i = k; i < n; ++i)
{
dp[0][i] = dp[2][i-1] + ((good(0, s[i])) ? 0 : 1);
if (!good((2 * k) % 3,s[i-k])) --dp[0][i];
minres = min(minres, dp[0][i]);
for(int c = 1; c <= 2; ++c)
{
dp[c][i] = dp[c-1][i-1] + ((good(c, s[i])) ? 0 : 1);
if (!good((2 * k + c) % 3,s[i-k])) --dp[c][i];
minres = min(minres, dp[c][i]);
}
}
cout << minres << endl;
}