题目
题目链接
题解及代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5+7;
int f[2*N][55],a[2*N],sum[2*N],ans[2*N],pb[2*N];
int T,n,k;
#define pr(x) cout<<#x<<":"<<x<<endl
int main(){
freopen("jumps.in","r",stdin);
cin>>T;
while(T--){
cin>>n>>k;
memset(f,0,sizeof(f));
memset(sum,0,sizeof(sum));
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
for(int i = 1;i <= n;++i){
char c;
scanf(" %c",&c);
if(c == '#') a[i] = 1;
sum[i] = sum[i-1] + a[i];
}
for(int i = n+1;i <= 2*n;++i){
a[i] = a[i-n];
sum[i] = sum[i-1] + a[i];
}
sum[2*n+1] = sum[2*n];
int pre = 0;
for(int i = 1;i <= 2*n;++i){
if(a[i]) pre = i;
else pb[i] = pre;
}
for(int i = 2*n;i;--i) for(int m = k;m;--m){
if(a[i]) continue;
int t = m + i;
if(t > 2*n){
if(!a[t-2*n] && sum[2*n]-sum[i]+sum[t-2*n] <= 1)
f[i][m] = 2*n+1;
}
else{
if(!a[t] && sum[t]-sum[i] <= 1)
f[i][m] = max(f[t][m],t);
}
}
for(int i = 1;i <= n;++i){
if(a[i]) continue;
int it = i;
for(int m = k;m;--m){
int nxt = f[it][m];
if(nxt) {
if(nxt > pb[i+n]) nxt = pb[i+n]+1;
ans[i] += (nxt-it) / m;
if((nxt-it)%m) ans[i]++;
it = nxt;
}
if(it > pb[i+n]) break;
}
if(it < pb[i+n]) ans[i] = -1;
}
for(int i = 1;i <= n;++i){
if(i != 1) cout<<' ';
cout<<ans[i];
}
cout<<endl;
}
return 0;
}