p1627 [CQOI2009]中位数
分析
https://www.luogu.org/blog/user43145/solution-p1627
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define li long long
const int N = 1e5;
li a[2*N+10],b[2*N+10],Ans=1;
int d[N+10];
int main(){
int n,m,i,j,k;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++){
scanf("%d",&d[i]);
if(d[i]<k)d[i]=-1;
else if(d[i]>k)d[i]=1;
else d[i]=0,j=i;
}
m=0;
for(i=j-1;i>0;i--){
m+=d[i];
a[m+N]++;
}
m=0;
for(i=j+1;i<=n;i++){
m+=d[i];
b[m+N]++;
}
Ans+=a[N]+b[N];
for(i=0;i<=2*N;i++)
Ans+=a[i]*b[2*N-i];
cout<<Ans;
return 0;
}