“玲珑杯”线上赛 Round #15 河南专场 咸鱼魔法记

DESCRIPTION

  给你一个01串,我们定义这个串的咸鱼值,是最长的全1串。现在你最多可以使用K次咸鱼魔法,每次魔法,你可以使得一个位置翻转(0变成1,1变成0)。问你这个串的咸鱼值最多是多少。

INPUT

  第一行两个整数N,K。表示串的长度和可以施展咸鱼魔法的次数。(N,K<=300000) 第二行N个01整数。

OUTPUT

  输出答案。
 

SAMPLE INPUT

  10 2
  1 0 0 1 0 1 0 1 0 1

SAMPLE OUTPUT

  5
 
 
前缀和+二分处理,先构造一个前缀和,再完后二分处理。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 const int MAX = 300010;
 6 int ans[MAX], n, k, num;
 7 int check(int pos){
 8     int l = pos, r = n;
 9     while(l <= r){
10         int m = (l+r)>>1;
11         if(m-pos+1 <= k+ans[m]-ans[pos-1])l=m+1;
12         else r = m-1;
13     }
14     return l-1;
15 }
16 int main(){
17     scanf("%d%d",&n,&k);
18     for(int i = 1; i <= n; i ++)
19         scanf("%d",&num), ans[i] = ans[i-1]+num;
20     int Max = 0;
21     for(int i = 1; i <= n; i ++){
22         int x = check(i);
23     //    printf("%d   %d\n",i,x);
24         if(Max < (x-i+1)){
25             Max = x-i+1;
26         }
27     }
28     printf("%d\n",Max);
29     return 0;
30 }

 

posted @ 2017-07-21 17:07  starry_sky  阅读(205)  评论(0编辑  收藏  举报