标签:
第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int b,n,a[100001],l[100001],r[200001],sum[200001],ans,pos; int read() { char c=getchar(); int a=0; while (c<‘0‘||c>‘9‘) c=getchar(); while (c>=‘0‘&&c<=‘9‘) { a=a*10+c-‘0‘; c=getchar(); } return a; } int main() { n=read();b=read(); sum[0]=0; for (int i=1;i<=n;i++) { a[i]=read(); if (a[i]>b) a[i]=1; else if (a[i]<b) a[i]=-1; else if (a[i]==b) { a[i]=0; pos=i; } } sum[pos]=0; l[n]=1; r[n]=1; for (int i=pos-1;i>=1;i--) { sum[i]=sum[i+1]+a[i]; l[sum[i]+n]++; } for (int i=pos+1;i<=n;i++) { sum[i]=sum[i-1]+a[i]; r[sum[i]+n]++; } ans=0; for (int i=0;i<=2*n-1;i++) ans+=l[i]*r[2*n-i]; printf("%d",ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4502308.html