现在的位置: 首页 > 综合 > 正文

hdu 4908 BestCoder Sequence

2015年01月15日 ⁄ 综合 ⁄ 共 489字 ⁄ 字号 评论关闭
# 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;
}

抱歉!评论已关闭.