博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
test 190731 codechef March Challenge COOKING SCHEDULE
阅读量:5098 次
发布时间:2019-06-13

本文共 2793 字,大约阅读时间需要 9 分钟。

Chef is a well-known chef, and everyone wishes to taste his dishes.

As you might know, cooking is not an easy job at all and cooking everyday makes the chef very tired. So, Chef has decided to give himself some days off.

Chef has made a schedule for the next N days: On i-th day if Ai is equal to 1 then Chef is going to cook a delicious dish on that day, if Ai is equal to 0 then Chef is going to rest on that day.

After Chef made his schedule he discovered that it's not the best schedule, because there are some big blocks of consecutive days where Chef will cook which means it's still tiring for Chef, and some big blocks of consecutive days where Chef is going to rest which means chef will be bored doing nothing during these days.

Which is why Chef has decided to make changes to this schedule, but since he doesn't want to change it a lot, he will flip the status of at most K days. So for each day which Chef chooses, he will make it 1 if it was 0 or he will make it 0 if it was 1.

Help Chef by writing a program which flips the status of at most K days so that the size of the maximum consecutive block of days of the same status is minimized.

Input

The first line of the input contains an integer T denoting the number of test cases.

The first line of each test case contains two integers: N denoting the number of days and K denoting maximum number of days to change.

The second line contains a string of length N , of which the i-th character is 0 if chef is going to rest on that day, or 1 if chef is going to work on that day

Output

For each test case, output a single line containing a single integer, which is the minimum possible size of maximum block of consecutive days of the same status achievable.

Constraints

  • 1 ≤ T ≤ 11,000
  • 1 ≤ N ≤ 106
  • The sum of N in all test-cases won't exceed 106.
  • 0 ≤ K ≤ 106
  • 0 ≤ Ai ≤ 1

Subtasks

  • Subtask #1 (20 points): N ≤ 10
  • Subtask #2 (80 points): Original Constraints

Example

Input:29 21100011114 11001Output:22

Explanation

Example case 1: The answer is 2 because we can change the string to: 110101011

Example case 2: If we don't change the input string at all, the answer will be 2. It is the best value we can achieve under the given conditions.

 

这么好写的二分题,考场上居然没有调出来【已经哭晕在厕所】

二分答案,假如有一段满足$gap_i \geq mid+1$,那么显然这里需要进行一次调整

调整次数为:$\frac{gap_i}{mid+1}$

这个很好想,每隔$mid$个相同的调整一个,如果恰好在最后一个位置要调整,改成在倒数第二个调整即可

如:当$mid=3$时

$111011101110\rightarrow111011101101$

需要特判为1的情况(因为若最后一位改成倒数第二位,倒数第三位和倒数第二位就会连起来)

 

 

#include
using namespace std;const int N=1e6+5;int n,k;char ss[N];vector
gap;bool check(int x){ int ret=0; for(int i=0;i
>1; if(check(mid)) r=mid; else l=mid; } printf("%d\n",r); } return 0;}

 

转载于:https://www.cnblogs.com/w19567/p/11276857.html

你可能感兴趣的文章
第十八章 30限制数组越界
查看>>
浅谈unique列上插入重复值的MySQL解决方案
查看>>
hdu 4859(思路题)
查看>>
11.2.0.4 sql*loader/oci direct load导致kpodplck wait before retrying ORA-54
查看>>
sql server 2008空间释放
查看>>
团队-科学计算器-最终总结
查看>>
树的遍历 TJUACM 3988 Password
查看>>
UVA 725 - Division
查看>>
bzoj1798: [Ahoi2009]Seq 维护序列seq(线段树)
查看>>
窗体中拖动panel,并且不会拖动至窗体外部程序实现方法。
查看>>
vb中从域名得到IP及从IP得到域名
查看>>
一步步跨过学习中一道道的坎
查看>>
妙味——操作元素属性的几种方法
查看>>
Ring 0 Inline Hook
查看>>
Linux man C++ 库函数
查看>>
PE结构对照表
查看>>
复杂性渐近阶的重要性
查看>>
js数组创建两种方法
查看>>
IOS自得其乐系列(一)-------------------加载动态图片
查看>>
Function Spec
查看>>