(枚举)consecutive(CD1678)
  W79oLdECuAdO 2023年11月02日 38 0


从一点开始最多能到多少个满足条件的.从开始点这样的搜索就行,



#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;


int go[1000000];

int main()
{
	int i,j,k;
	int t;
	int n;
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d",&n,&k);
		for (i=1;i<=n;i++)
			scanf("%d",go+i);
		go[0]=0;
		for (i=2;i<=n;i++)

		{
			go[i]+=go[i-1];
		}
		if (go[n]+k>=n)
		{
			printf("%d\n",n);
			continue;
		}
		int ans=0;
		j=1;
		i=0;
		while (j<=n)
		{
			//cout<<i<<' '<<j<<' '<<ans<<' '<<go[j]-go[i]<<endl;
			if (go[j]-go[i]+k>j-i)
			{
				j++;
				ans=max(ans,j-i);
			}
			else if (go[j]-go[i]+k==j-i)
			{
				if (j<n&&go[j+1]-go[j]==1)
				{
					j++;
					//cout<<"one";
					ans=max(ans,j-i);
				}
				else if (j<n)
				{
					//cout<<"two";
					i++;
				}
				else 
				{
					j++;
					//cout<<"three";
				}
			}
			else
				i++;
			//cout<<i<<' '<<j<<' '<<ans<<endl<<endl;
		}
		printf("%d\n",ans);
	}
}




consecutive

Time Limit: 3000 ms Memory Limit: 65535 kB Solved: 45 Tried: 156


Description



You are given a 01 sequence whose size is n. you can change at most k 0 into 1 in the sequence.
Now I want to know how many consecutive 1 in the sequce at most after you do the change.



Input



The first of input is an integer t which stands for the number of test cases.
for each test case, the first line contains two number n, k ( 1 <= n <= 100000, 0 <= k <= n ).
the next contains n element of the sequence, whose value is 0 or 1.



Output



Output the answer in one line for each test case.



Simple Input



2
4 2
1 0 0 1
4 1
1 0 1 0



Simple Output



4
3



Source




【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读