zoukankan      html  css  js  c++  java
  • Black & White(尺取)

    链接:https://ac.nowcoder.com/acm/contest/893/F
    来源:牛客网

    * 第一行一个整数 T ,表示接下来有 T 个样例。
    * 首先输入n,m,表示S串的长度n和操作次数m,其中1n1000001≤n≤100000,0m10000≤m≤1000;
    * 接下来输入一个长度为n的字符串S。

    输出描述:

    一个整数,表示题面上描述的最大价值。
    示例1

    输入

    复制
    2
    5 1
    00101
    2 1
    01

    输出

    复制
    4
    2

    说明

    第一个串翻转第三个位置,00001的价值为4;第二个串翻转第一个位置,11的价值为2。
    思路:记录每个串中1的位置和0的位置,然后尺取
    代码:
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    string str;
    int l[maxn];
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            cin>>str;
            l[0]=-1;
            int cnt=1;
            for(int t=0;t<n;t++)
            {
             if(str[t]=='1')
             {
               l[cnt++]=t;
             }    
            }
            l[cnt++]=n;
        //    cout<<cnt<<endl;
            if(m>=cnt-2) 
            {
            printf("%d
    ",n);
            continue;
            }
            int ans=0;
            for(int t=1;t<cnt-m;t++)
            {
                ans=max(ans,l[t+m]-l[t-1]-1);
            }
            cnt=1;
            l[0]=-1;
            for(int t=0;t<n;t++)
            {
             if(str[t]=='0')
             {
               l[cnt++]=t;
             }    
            }
            l[cnt++]=n;
            if(m>=cnt-2)
            {
                printf("%d
    ",n);
                continue;
            }
            for(int t=1;t<cnt-m;t++)
            {
                ans=max(ans,l[t+m]-l[t-1]-1);
            }
            cout<<ans<<endl; 
            
        }
        return 0;
    }
  • 相关阅读:
    sql server 获取本月的始末时间
    超时时间已到
    sql server定时自动备份
    创建连接服务器
    date制作电子时钟
    C#继承(三)
    C# Split分割
    Dom动态添加属性
    date制作电子时钟(二)
    全局遮罩 shade
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10821929.html
Copyright © 2011-2022 走看看