玲珑学院 1124 咸鱼魔法记

1124 - 咸鱼魔法记
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
标记0的坐标,求取最长区间
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int n,k,x;
int a[300010];
int main()
{
    scanf("%d%d",&n,&k);
    a[0]=0;
    int ans=-1,pos=0,cnt=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if(x==0)
        {
            a[cnt++]=i;
            ans=max(ans,pos);
            pos=0;
        }
        else pos++;
    }
    if(k==0) printf("%d
",ans);
    else if(k>=(cnt-1)) printf("%d
",n);
    else
    {
        for(int i=k+1;i<cnt;i++)
        {
            ans=max(ans,a[i]-a[i-k-1]-1);
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7217740.html