可惜没如果=_=
时光的河入海流

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 2988  Solved: 1846
[Submit][Status][Discuss]

Description

给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。

Input

第一行为两个正整数n和b ,第二行为1~n 的排列。

Output

输出一个整数,即中位数为b的连续子序列个数。

Sample Input

7 4
5 7 2 4 3 1 6

Sample Output

4

HINT

第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000

Source

前缀和乱搞搞就好了qwq

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX=1e5+5;
 5 LL n,m,p,s[MAX],a[MAX],ans;
 6 inline LL read(){
 7     LL an=0,x=1;char c=getchar();
 8     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
 9     while (c>='0' && c<='9') {an=(an<<3)+(an<<1)+c-'0';c=getchar();}
10     return an*x;
11 }
12 int main(){
13     freopen ("number.in","r",stdin);freopen ("number.out","w",stdout);
14     LL i,j,x;
15     n=read();m=read();
16     for (i=1;i<=n;i++){
17         x=read();
18         if (x<m) s[i]=-1;
19         else if (x>m) s[i]=1;
20         else p=i;
21         s[i]+=s[i-1];
22     }
23     for (i=0;i<p;i++) a[s[i]+n]++;
24     for (j=p;j<=n;j++) ans+=a[s[j]+n];
25     printf("%lld",ans);
26     return 0;
27 }

 

posted on 2017-11-06 16:04  珍珠鸟  阅读(115)  评论(0编辑  收藏  举报