K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)

题意:

给定长为 n 的 0, 1 字符串,你可以通过一次操作改变一个字符(0 变 1 or 1 变 0),问最少几次操作可以使任意相邻两个 1 之间的距离为 k ?

题目:

You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string si equals ‘0’ if the i-th lamp is turned off or ‘1’ if the i-th lamp is turned on. You are also given a positive integer k.

In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).

The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands “00010010”, “1001001”, “00010” and “0” are good but garlands “00101001”, “1000001” and “01001100” are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.

Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤25 000) — the number of test cases. Then t test cases follow.

The first line of the test case contains two integers n and k (1≤n≤106;1≤k≤n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters ‘0’ and ‘1’.

It is guaranteed that the sum of n over all test cases does not exceed 106 (∑n≤106).

Output

For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one.

Example

Input

6
9 2
010001010
9 3
111100000
7 4
1111111
10 3
1001110101
1 1
1
1 1
0

Output

1
2
5
4
0
0

Solution:#

显然根据题意可以得知最终所有 1 的位置 mod k 的余数 t 都相同。那么我们就可以去枚举 t,判断在这种情况下,需要几次操作,最后取 min 即可。
  首先我们要知道的是我们最多改变多少次:原字符串 1 的个数 −1 次。
  我们先算出原字符串 1 的个数 cnt。然后假设原字符串的 1 对应 1,0 对应 −1,那么我们将对应位即:t, t+k, t+2∗k, … 的字符提取出来,按照对应方式组成一个新序列,那么求出该序列的最大连续子段和 cur,那么 min(cnt−cur) 就是答案。
  我们知道只有 t, t+k, … 这些位才有可能为 1,那么我们求出的 cur 无非就两种情况:
  1. a, b, c (a, c是一连串的 −1; b 是一连串的 1);
  2. a, b, c (a,c 是一连串的 1; b 是一连串的 −1)(a+b+c>max(a,c))。
  对于 1. 来说,cur 个 1 是不需要改变的,那么在其他的位置多出来 cnt−cur 个 1,所以需要 cnt−cur 次来把他们变为 0。
  对于 2. 来说,有 a+c 个 1 是不需要改变的,我们需要把中间的 b 个 0 变为 1,还需要把 cnt−a−c 个其他位置的 1 变为 0。即 b+cnt−a−c=cnt−cur(cur=a+c+b)。
  求出最大子段和其实就是最多不需要改变的个数(新序列)。

AC代码:

/**题目大意
给你一个01串,要你用最少的变化次数使得所有的1相邻的距离为k。
变化的方式为1->0,0->1.要你求最少的变化次数
题目思路
emm完全没啥思路,看了题解,其实就是你要想这些数字1都
是modk等于一个定值,那么你就可以用余数开始枚举。
首先肯定时要统计所有1的个数 sum
然后把 ai 中的字符 1 看成数字 1,字符 0 看成数字 −1,
问题就变成了求 ai 中最大连续子序列和 (now)sum-now即为最优解。
有些难解释,但是自己仔细思考应该就明白了。遇到此类题目就是要
枚举余数*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
char s[maxn];
int t,n,k,sum,now,ma;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        sum=0,ma=0;
        scanf("%d %d %s",&n,&k,s);
        for(int i=0; i<=n-1; i++)
        {
            if(s[i]=='1')
                sum++;
        }
        for(int i=0; i<=k-1; i++)
        {
            now=0;
            for(int j=i; j<=n-1; j=j+k) //1->1 0->-1
            {
                int x=2*(s[j]-'0')-1;
                now+=x;
                if(now<0)
                    now=0;
                ma=max(ma,now);//最大连续的值
            }
        }
        printf("%d\n",sum-ma);
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值