题意描述
思路
AC代码
代码语言:javascript
复制#include<bits/stdc .h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x )
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1e6 10;
const int M=1e6 10;
const int INF=0x3f3f3f3f;
const int MOD=1e9 7;
/*注意边界
清空每个case的影响
注意数据范围
注意证明*/
char s[N];
int sum[N],f[N];
void solve(){
int n,k;cin>>n>>k;
cin>>s 1;
rep(i,1,n 1){
if(s[i]=='1'){
sum[i]=sum[i-1] 1;
}else sum[i]=sum[i-1];
}
if(sum[n]<=1){
cout<<0<<endl;
return;
}
int ans=sum[n]-1;
rep(i,1,n 1){
f[i]=sum[i-1] (s[i]!='1');
if(i>k) f[i]=min(f[i],f[i-k] sum[i-1]-sum[i-k] (s[i]!='1'));
//如果最后的代价在前面加就会导致多次计算最后的代价
ans=min(ans,f[i] sum[n]-sum[i]);
}
cout<<ans<<endl;
}
int main(){
IOS;
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int t;cin>>t;
while(t--)
solve();
return 0;
}