题意:

给定长为 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;
}

K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)相关推荐

  1. K-periodic Garland CodeForces - 1353E(贪心)

    You are given a garland consisting of n lamps. States of the lamps are represented by the string s o ...

  2. Codeforces Round #699 (Div. 2) E.Sorting Books(贪心+DP / 线段树)超高质量题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 E - Sorting Books 一排书架上有 nnn 本书排成一排,每本书上有一个颜色 aia_i ...

  3. 尽梨了(贪心+dp)

    尽梨了 problem solution code problem 丰之崎学园附近共有 nnn 个商店,在时刻 000 时,英梨梨从学园出发开始购物. 从学园走到任意一个商店,或从一个商店走到另一个商 ...

  4. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  5. URAL 1203 Scientific Conference(贪心 || DP)

    Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP.多校如果有计算几何一定要干掉-.- 题意:给 ...

  6. Codeforces 919D Substring (拓扑图DP)

    Codeforces 919D Substring (拓扑图DP) 手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo ...

  7. 【bzoj3174】[Tjoi2013]拯救小矮人 贪心+dp

    题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人,我们知道他从脚 ...

  8. CodeForces 711C - Coloring Trees DP

    /*http://codeforces.com/problemset/problem/711/C http://codeforces.com/blog/entry/46830官方题解:We compu ...

  9. [CodeForces 332B]Maximum Absurdity[DP]

    题目链接: [CodeForces 332B]Maximum Absurdity[DP] 题意分析: 寻找两个不重叠的长度为k的子串,使得它们之和最大. 解题思路: 第一想法是,处理出从这个点开始,长 ...

最新文章

  1. 力扣(LeetCode)刷题,简单题(第11期)
  2. 最佳实践系列丨Docker EE 服务发现参考架构(二)
  3. hive读取hdfs存放文件_Hive基本概念
  4. skyline的用法
  5. 【Accelerated C++】重点回顾
  6. Windows下Core Audio APIs的使用简介
  7. io操作是指什么_各种IO模型,一篇打尽
  8. 最受互联网争抢的web前端工程师
  9. AI 人才缺失催生跨境猎头:人才年薪高达 300 万,猎头直赚 100 万
  10. Android4.2之Camera系统HAL调用流程
  11. Weblogic配置和部署
  12. 【LKJ】LKJ弧形限速小结
  13. SpringBoot 3.0最低版本要求的JDK 17,这几个新特性不能不知道
  14. 如何做一个自己的网站?
  15. 评分卡模型(一)评分卡建模实战
  16. Tuxera Disk Manager是什么软件,Tuxera Disk Manager怎么用
  17. centos查看oracle版本,查看linux系統版本信息(Oracle Linux、Centos Linux、Redhat Linux、Debian、Ubuntu)...
  18. 关于 C4D R18 载入闪退
  19. android自定义Drawable实现炫酷UI-锦鲤游泳效果
  20. 光流文件(.flo)转图像

热门文章

  1. Android之在笔记本电脑adb devices识别不了oppo A9手机(设备管理器ADB Interface里面有个黄色感叹号)
  2. linux之vim复制多行、光标跳转到指定行、插入当前光标上和下行
  3. C++之delete常见错误总结
  4. C语言之struct A *b和struct A c区别
  5. linux 配置path
  6. 【C语言简单说】十二:逻辑运算符
  7. 2020美国纽约大学计算机科学排名,2020美国纽约大学排名第几
  8. wait放弃对象锁_Java线程中wait、await、sleep、yield、join用法总结
  9. 球体表面积原来还可以这么求!
  10. 北大保送、硕博连读!《西游记》红孩儿扮演者现成中科院博士!