题目链接:点击打开链接
题意:给你n个数和m,问在这n个数中以m为中位数的区间有多少个?
因为要使m为中位数,肯定是m的值位于这些数的中间的部分,即要有比m大的数和比m小的数,且大的数和小的数要相等或者大的数比小的数多一
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin >> n>>m;
vector<int>p(n);
for(int i=0; i<n; i++)
cin>>p[i];
map<int,int>mp;
mp[0]=1;
bool has=false;
int sum=0;
long long ans=0;
for(int r=0; r<n; r++)
{
if(p[r]<m)
sum--;
else if(p[r]>m)
sum++;
if(p[r]==m)
has=true;
if(has)
ans+=mp[sum]+mp[sum-1];
else
mp[sum]++;
}
cout<<ans<<endl;
return 0;
}