#include <bits/stdc++.h>
using namespace std;
const int N= 1e6 + 5;
char a[N];
int dp[N],s[N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, k;
scanf("%d%d", &n, &k);
scanf("%s", a + 1);
dp[0]=dp[1]=0;
s[0]=0;
for(int i = 1; i <= n; i++)
{
//1的个数
s[i] = s[i - 1] + (a[i] == '1');
//把前面的1都抹掉
dp[i] = s[i - 1];
//如果大于等于k,默认当前位置是1
if(i >= k)
// 上一个1的位置
dp[i] = min(dp[i], dp[i - k] + s[i - 1] - s[i - k]);
//如果是1,再加上代价
if(a[i] == '0')
dp[i]++;
}
int ans = 1e9;
//只是到i,把i后面的1都抹掉
for(int i = 0; i <= n; i++)
ans = min(ans, dp[i] + s[n] - s[i]);
printf("%d\n", ans);
}
return 0;
}
相关文章
- 04-11Codeforces Round #642 (Div. 3) E—K-periodic Garland dp
- 04-11Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)
- 04-11Codeforces Round #656 (Div. 3)E. Directing Edges(拓扑排序+构造dag图)
- 04-11Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp
- 04-11Codeforces Round #693 (Div. 3) G - Moving to the Capital 最短路+dp
- 04-11Codeforces Round #674 (Div. 3) F. Number of Subsequences 题解(dp)
- 04-11Codeforces Round #674 (Div. 3) (A - E题题解)
- 04-11Codeforces Round #635 (Div. 2) E——Kaavi and Magic Spell 区间dp
- 04-11Codeforces Round #748 (Div. 3) (D1 - E)
- 04-11Codeforces Round #692 (Div. 1, based on Technocup 2021 Elimination Round 3)(A-E) 题解