hdu 4908 BestCoder Sequence

时间:2023-03-09 05:26:48
hdu 4908  BestCoder Sequence
# include <stdio.h>
# include <algorithm>
using namespace std;
int main()
{
int n,m,i,sum,cot,flag,j;
int map[80040];
int a[40010];
int ans1;
int ans2;
while(~scanf("%d%d",&n,&m))
{
ans1=ans2=40010;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==m)
flag=i;
}
cot=0;
map[ans2]=1;
for(j=flag+1;j<=n;j++)
{
if(a[j]>m)
map[++ans1]++; //记录出现该状态的次数
else
map[--ans1]++;
}
cot+=map[ans2];//当状态数为ans2,才满足中位数
for(j=flag-1;j>=1;j--)
{
if(a[j]<m)
{
cot+=map[++ans2];
}
else
{
cot+=map[--ans2];
}
}
printf("%d\n",cot);
}
return 0;
}