-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathE.cpp
More file actions
134 lines (120 loc) · 1.93 KB
/
E.cpp
File metadata and controls
134 lines (120 loc) · 1.93 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
#define ll long long
#define fab(a,b,i) for(int i=a;i<b;i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define quick ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
int dp[2*1000005];
int suff[2*1000005];
int cnt[2*1000005]={0},cnt1[2*1000005];
int n,k;
string s;
int recurse(int pos)
{
if(pos>=n)
return 0;
if(dp[pos]!=-1)
return dp[pos];
if(pos+k>=n)
{
return dp[pos]=min(suff[pos],cnt[pos]+s[pos]-'0');
}
else
{
return dp[pos]=min(suff[pos]+recurse(pos+k),cnt[pos]+s[pos]-'0');
}
}
int main()
{ quick;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;
while(t--)
{
//ll int n,k;
cin>>n>>k;
//string s;
cin>>s;
while(n>0 and s[n-1]=='0')
n--;
if(n<2)
{
cout<<0<<endl;
continue;
}
int st=0;
fab(0,n,i)
{
if(s[i]=='1')
{
st=i;
break;
}
}
fab(0,n+k+1,i)
{dp[i]=-1;
suff[i]=0;
cnt[i]=0;
cnt1[i]=0;
}
int tot=0;
for(int i=n-1;i>=0;i--)
{
if(s[i]=='0')
{
//suff[i]=1+(cnt);
cnt[i]=tot;
suff[i]=1+(cnt[i]-cnt[i+k-1]);
}
else if(s[i]=='1')
{
//cout<<"SGFD"<<i<<endl;
//suff[i]=(cnt-suff[i+k]);
cnt[i]=tot;
suff[i]=(cnt[i]-cnt[i+k-1]);
tot++;
}
// cout<<cnt[i]<<" "<<i<<" "<<tot<<endl;
}
//fab(0,n,i)
//cout<<suff[i]<<" ";
//cout<<endl;
int pp=0;
fab(0,n,i)
{
recurse(i);
cnt1[i]=pp;
if(s[i]=='1')
pp++;
}
int mn=count(all(s),'1')-1;
fab(0,n,i)
{
/*int ut=0,tt=0;
int j=i-k;
while(j>=st)
{
if(s[j]=='1')
ut++;
else
tt++;
j-=k;
}*/
//cout<<dp[i]<<" "<<cnt1[i]<<" "<<ut<<" "<<tt<<" "<<cnt[i]<<endl;
mn=min(mn,(dp[i]+cnt1[i]));
}
cout<<mn<<endl;
}
return 0;
}