首先找到b的位置x,要求满足b为中位数的数列
把比b大的设为1,比b小设为-1
则数列满足条件则x 左边的的和+x右边的和=0 
然后应该就很好搞了

 1 var a,l,r,s:array[0..200010] of longint;
 2     i,n,m,x,k,ans:longint;
 3 begin
 4   readln(n,m);
 5   for i:=1 to n do
 6   begin
 7     read(x);
 8     if x=m then k:=i
 9     else if x>m then a[i]:=1
10     else a[i]:=-1;
11   end;
12   l[n]:=1;
13   r[n]:=1;
14   for i:=k-1 downto 1 do
15   begin
16     s[i]:=s[i+1]+a[i];
17     inc(l[s[i]+n]);
18   end;
19   for i:=k+1 to n do
20   begin
21     s[i]:=s[i-1]+a[i];
22     inc(r[s[i]+n]);
23   end;
24   for i:=0 to 2*n-1 do
25     ans:=ans+l[i]*r[2*n-i];
26   writeln(ans);
27 end.
View Code

 

posted on 2014-10-20 18:23  acphile  阅读(143)  评论(0编辑  收藏  举报