zoukankan      html  css  js  c++  java
  • codefroces 873 B. Balanced Substring && X73(前缀和思想)

    B. Balanced Substring

    You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

    You have to determine the length of the longest balanced substring of s.

    Input

    The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

    The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.

    Output

    If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

    Examples
    Input
    8
    11010111
    Output
    4
    Input
    3
    111
    Output
    0
    Note

    In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

    In the second example it's impossible to find a non-empty balanced substring.

    前缀和离散化

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <cstdlib>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #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.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    char s[100005];
    int a[200005],n,ans=0,val=0;
    int main()
    {
        scanf("%d%s",&n,&s);
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            ans+=s[i]=='0'?-1:1;
            if(ans==0) val=i+1;
            else
            {
                if(!a[ans+n]) a[ans+n]=i+1;
                else val=max(val,i-a[ans+n]+1);//若中间值存在必为0,因此左右指针对应的值相等
            }
        }
        printf("%d
    ",val);
        return 0;
    }

    给你一个数组,求数组中连续区间内和为73的区间个数

    输入n(0<n<=100000),接下来输入n个数ai(0<ai<75);

    输出

    连续区间内和为73的区间个数,若没有输出"X"

    前缀和维护数组。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <cstdlib>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #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.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    ll val[100005],n,ans=0,k=0;
    map<ll,ll>a;
    int main()
    {
        scanf("%lld",&n);
        a[0]=1;
        for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
        for(int i=1;i<=n;i++)
        {
            ans+=val[i];
            a[ans]++;
            if(ans>=73) k+=a[ans-73];
        }
        if(k) printf("%lld
    ",k);
        else printf("X
    ");
        return 0;
    }
  • 相关阅读:
    node.js系列:(调试工具)node-inspector调试Node.js应用
    NodeJs系列一:神奇的nodejs
    [原创] JavaScript实现简单的颜色类标签云
    jquery实现图片上传前本地预览
    前端制作入门知识
    解决pycharm中加载本地网页,弹出“requested without authorization”提示框不能加载网页的问题
    Mac中的Python安装selenium,结合chrom及chromdriver使用
    面试题(一)
    Python脚本之——API自动化框架总结
    红日ATT&CK红队评估实战靶场1
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7666887.html
Copyright © 2011-2022 走看看