codefroces 873 B. Balanced Substring && X73(前缀和思想)
Posted 十年换你一句好久不见
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codefroces 873 B. Balanced Substring && X73(前缀和思想)相关的知识,希望对你有一定的参考价值。
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.
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.
If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.
8
11010111
4
3
111
0
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\n",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\n",k); else printf("X\n"); return 0; }
以上是关于codefroces 873 B. Balanced Substring && X73(前缀和思想)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 873 B. Balanced Substring(前缀和 思维)
Codefroces 852 G. Bathroom terminal
Codefroces1077F2. Pictures with Kittens (hard version)
codefroces Round #201.a--Difference Row