Cooking Schedule Problem Code: SCHEDULE

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:

2
9 2
110001111
4 1
1001
Output:

2
2
思路:
用大根堆存连续相同序列的长度,同时存下标号及切割次数(为了在最长的连续序列相同的前提下先切切割次数少的,因此要先用一个大一些的数代表切割0次,每切割一次这个数减1),用另一个数组记录这个序列的最初长度。每次切割长度最长的序列,长度变成最初的长度/(切割次数+1),再次加进堆(只需加一段即可)。直到剩下的最长长度只有2。对于小于2的情况,特殊处理即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
int _;
int n,k,a[];
char c[];
priority_queue <pair<int,pair<int,int>>> q;
int main()
{
scanf("%d",&_);
while (_--)
{
scanf("%d%d",&n,&k);
scanf("%s",c);
while (!q.empty()) q.pop();
int tot=,cnt=;;
int i;
for (i=;i<n;i++)
if (c[i]==c[i-]) tot++;
else
{
//cout<<tot<<endl;
q.push({tot,{,cnt}});
a[cnt]=tot;
cnt++;
tot=;
}
q.push({tot,{,cnt}});
a[cnt]=tot;
cnt++;
if (q.top().first==)
{
puts("");
continue;
}
char p='';
tot=;
int len=strlen(c);
for (i=;i<len;i++)
{
if (c[i]!=p) tot++;
if (p=='') p=''; else p='';
}
if (tot<=k)
{
puts("");
continue;
}
p='';
tot=;
for (i=;i<len;i++)
{
if (c[i]!=p) tot++;
if (p=='') p=''; else p='';
}
if (tot<=k)
{
puts("");
continue;
}
//cout<<"hhhhhhhhhh"<<endl;
//cout<<q.top()<<endl;
int x;
while (k--)
{
x=q.top().first;
if (x<=) break;
int ix=q.top().second.second;
int nval=a[ix];
int id=q.top().second.first;
id--;
int im=-id;
x=nval/(im+);
q.pop();
q.push({x,{id,ix}});
}
printf("%d\n",q.top().first);
}
return ;
}

Cooking Schedule Problem Code: SCHEDULE(优先队列)的更多相关文章

  1. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDOJ 1534 Schedule Problem 差分约束

    差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...

  3. Maker's Schedule, Manager's Schedule

    http://www.paulgraham.com/makersschedule.html manager's schedule 随意性强,指随时安排会面,开会等活动的 schedule; maker ...

  4. POJ 3553 Task schedule【拓扑排序 + 优先队列 / 贪心】

    Task schedule Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 515 Accepted: 309 Special J ...

  5. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

  6. Schedule Problem spfa 差分约束

    题意:有n个任务,给出完成n个任务所需时间,以及一些任务安排.任务安排有四种: FAS a b:任务a需在任务b开始后完成. FAF a b:任务a需在任务b完成后完成. SAF a b:任务a需在任 ...

  7. HDU-1534 Schedule Problem

    四种约束条件..照做就行了.. 最长路建图. #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  8. lr11.0负载测试 real-world schedule 与basic schedule的区别是什么

    real-world schedule 是真实场景模式  可以通过增加ACTION来增加多个用户 basic schedule 是我们以前用的 经典模式  只能设置一次负载的上升和下降

  9. Holes in the text Add problem to Todo list Problem code: HOLES

    import sys def count_holes(letter): hole_2 = ['A', 'D', 'O', 'P', 'Q', 'R'] if letter == 'B': return ...

随机推荐

  1. FPGA编程技巧系列之按键边沿检测

    抖动的产生: 通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开.因而在闭合及断开的瞬间均伴随有一连串的 ...

  2. Dreamoon and MRT

    Dreamoon and MRT 题目链接: http://codeforces.com/group/gRkn7bDfsN/contest/212299/problem/B 只需要考虑相对位置,设a0 ...

  3. 部署 k8s Cluster(上)[转]

    我们将部署三个节点的 Kubernetes Cluster. k8s-master 是 Master,k8s-node1 和 k8s-node2 是 Node. 所有节点的操作系统均为 Ubuntu ...

  4. Qt_为什么学习Qt

    1)学习GUI编程,市场上任何一款产品几乎都带有图形界面,市场上很火的Androoid.IOS编程无非也是GUI app编程,GUI编程都是差不多的,学习Qt后再学习ANdroid IOS ,那都是S ...

  5. Java中的类加载器--Class loader

    学习一下Java中的类加载器,这个是比较底层的东西,好好学习.理解一下.  一.类加载器的介绍 1.类加载器:就是加载类的工具,在java程序中用到一个类,java虚拟机首先要把这个类的字节码加载到内 ...

  6. 三、C++ const分析

    1.C语言中的const: const修饰的变量是只读的,本质还是变量 const修饰的局部变量在栈上分配空间 const修饰的全局变量在只读存储区分配空间 const只在编译期有用,在运行期无效 c ...

  7. ubuntu修改网卡名称ensX为eth0

    1.sudo nano /etc/default/grub 找到GRUB_CMDLINE_LINUX="" 改为GRUB_CMDLINE_LINUX="net.ifnam ...

  8. 如何用纯 CSS 创作一个容器厚条纹边框特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/YLqbXy 可交互视频教程 此视 ...

  9. 比特币 3角对冲python代码

    3角对冲原理 基础货币 base, 兑换货币 quote, 中间货币 mid. 市场分为3个市场 p3: base_quote p2: quote_mid p1: quote_mid 代码逻辑 1, ...

  10. 项目:开发->测试->上线:流程规范

    1) 项目分析 2) 项目经理分工协作 程序就具体流程: 1: 按项目名称: 建git, 数据库, 线上测试虚拟机 2: 按项目经理分配的大的模块. 自行划分工作阶段 a: 划分为小的模块 b: 预估 ...