Codeforces Round #642 (Div. 3) E—K-periodic Garland dp
作者:互联网
#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;
}
标签:Garland,int,scanf,d%,Codeforces,char,642,dp 来源: https://www.cnblogs.com/QingyuYYYYY/p/12902324.html