[Explanations] Median luogu_P1627_ (arranged promiscuity

First, we focus on the relationship between the size of the number, so the set is less than b-1, b is set to be greater than 1, and as long as the interval containing 0 and b is the legitimate

Record number of left and right sum that appears when statistics in buckets, to take left + right == 0 up to answer in mind

They are relatively routine, right

#include<bits/stdc++.h>
using namespace std;
const int maxn=200009;
int n,b,a[maxn],p,ans;
int sum[maxn],lc[maxn],rc[maxn];
int main(){
    scanf("%d%d",&n,&b);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]<b)a[i]=-1;
        if(a[i]>b)a[i]=1;
        if(a[i]==b)p=i;
    }
    lc[n]=1;rc[n]=1;
    for(int i=p+1;i<=n;i++){
        sum[i]=sum[i-1]+a[i];
        lc[n+sum[i]]++;
    }
    for(int i=p-1;i>=1;i--){
        sum[i]=sum[i+1]+a[i];
        rc[n+sum[i]]++;
    }
    for(int i=-n;i<=n-1;i++){
        ans+=lc[i+n]*rc[-i+n];
    }
    printf("%d",ans);
}

 

Guess you like

Origin www.cnblogs.com/superminivan/p/11730238.html