• 玲珑学院 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;
    }
  • 相关阅读:
    Linux配置dhcp自动获取ip地址
    CentOS7密码复杂度配置
    JDK切换和BurpSuite破解
    python操作Elasticsearch7.x
    mongoShake docker 部署 (mongoshake:2.6.5)
    elasticsearch 7.x 安装使用(ik,elasticsearch head )
    mongoDB -- 全文索引
    商业模式画布
    Vue全家桶--12 Vue-CLI 3.x 脚手架构建项目
    Vue全家桶--11 Webpack和Vue-Loader打包资源
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7217740.html
Copyright © 2020-2023  润新知