Topic links: the Click here Wallpaper
Solution:
Appears to be greedy, it is not. . .
We define \ (f [i] \) represents only covers \ (1 \ sim i \) a minimum cost required, then the \ (I \) is the 0 point, the readily available \ (f [i] = min (f [i], f [i-1] + i) \)
Consider when \ (i \) is a how to do that when \ (i \) when 1, by definition, we do not transfer \ (i \) the value of the position, and transfer \ (i + k \) in this position the value
Obviously, as long as the \ (1 \ sim p (ik \ le p \ le i + k-1) \) has been covered, then reselection \ (I \) , \ (. 1 \ SIM I + K \) can be It is covered
We maintain the interval with a segment tree \ (f \) minimum, each transfer can find the minimum value of the transfer. Finally, note that determine the boundary conditions.
Code:
#include<bits/stdc++.h>
#define ls q<<1
#define rs q<<1|1
#define int long long
using namespace std;
const int N=2e5+1;
const int maxn=1e15;
char s[N];
int n,k,f[N],mn[N<<2];
int min(int a,int b){return b<a?b:a;}
int max(int a,int b){return b<a?a:b;}
void update(int q){mn[q]=min(mn[ls],mn[rs]);}
void ins(int q,int l,int r,int x,int v){
if(l==r) return mn[q]=v,void();
int mid=l+r>>1;
if(mid>=x) ins(ls,l,mid,x,v);
else ins(rs,mid+1,r,x,v);
update(q);
}
int query(int q,int l,int r,int L,int R){
if(R<L) return 1e18;
if(l>=L&&r<=R) return mn[q];
int mid=l+r>>1,re=maxn;
if(mid>=L) re=min(re,query(ls,l,mid,L,R));
if(mid<R) re=min(re,query(rs,mid+1,r,L,R));
return re;
}
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
signed main(){
n=read(),k=read();
scanf("%s",s+1);
memset(mn,127,sizeof(mn));
memset(f,127,sizeof(f));f[0]=0;
for(register int i=1;i<=n;i++){
if(s[i]=='1'){
int p=min(n,i+k);
int v=query(1,1,n,max(1,i-k-1),p-1);
if(i-k-1<=0) f[p]=min(f[p],i);
f[p]=min(f[p],v+i);ins(1,1,n,p,f[p]);
}else f[i]=min(f[i],f[i-1]+i),ins(1,1,n,i,f[i]);
}
printf("%lld\n",f[n]);
return 0;
}