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;
    }
  • 相关阅读:
    Net设计模式实例之命令模式(Command Pattern)
    Jquery实现回车键Enter切换焦点
    Asp.net MVC2.0系列文章显示列表和详细页面操作
    [入门篇]Jquery读取.Net WebService Json数据
    Net设计模式实例之中介者模式(Mediator Pattern)
    26个Jquery使用小技巧(jQuery tips, tricks & solutions)
    虚拟化让私有云逐步落地
    Jquery打造AdRotator轮转图片
    360度全方位沟通向上领导法则一
    使用Jquery+CSS如何创建流动导航菜单Fluid Navigation
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7666887.html
Copyright © 2011-2022 走看看