bzoj1303: [CQOI2009]中位数图

题目

Solution

因为我们只关心相对大小,所以数字可以转为1,-1,0。要求覆盖b位置的总和为0的连续子序列数量

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a,b,i,s,ri,sum[2][200001];
ll ans;
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){
    int x=0,fl=1;char ch=gc();
    for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
    for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
    return x*fl;
}
int main(){
    n=read();b=read();
    sum[0][n]=1;
    for (i=0,s=n;i<n;i++){
        a=read();
        if (a!=b) s+=a>b?1:-1;
        else ri=1;
        sum[ri][s]++;
    }
    for (i=0,n<<=1;i<n;i++) ans+=(ll)sum[0][i]*sum[1][i];
    printf("%lld",ans);
}

猜你喜欢

转载自blog.csdn.net/xumingyang0/article/details/81195331