小b有一个01序列,她想找到一个最长的区间使得这个区间的01能两两配对,即0的个数和1的个数相等。求最长区间的长度。
输入
第一行输入一个正整数n;
第二行输入n个整数,表示A[i],以空格隔开;
第三行输入一个正整数K;
其中1≤n≤30000,对于任意A[i]有-10000≤A[i]≤10000,2≤K≤10000
输出
输出一个数,表示子序列的数目
输入样例
3
0 1 0
输出样例
2
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10,M=5e4;
#define int long long
int a[N],vis[N];
signed main(){
int n;
cin>>n;
int sum=0,ans=0;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]==1)sum++;
else sum--;
if(sum==0)ans=max(ans,i);
else
if(vis[sum+M]==0)vis[sum+M]=i;
else ans=max(ans,i-vis[sum+M]);
}
cout<<ans<<endl;
}