BZOJ1303: [CQOI2009]中位数图

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

 
将大于b的数设为1,小于b的数设为-1,b对应的数设为0。
前缀后缀搞搞就行了。
#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
    if(head==tail) {
        int l=fread(buffer,1,BufferSize,stdin);
        tail=(head=buffer)+l;
    }
    return *head++;
}
inline int read() {
    int x=0,f=1;char c=Getchar();
    for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
    return x*f;
}
const int maxn=100010;
int n,b,p,A[maxn],sum[maxn],L[maxn*2];
long long ans;
int main() {
    n=read();b=read();
    rep(i,1,n) {
        A[i]=read();
        if(A[i]==b) p=i,A[i]=0;
        else A[i]=A[i]>b?1:-1;
    }
    rep(i,p,n) L[(sum[i]=sum[i-1]+A[i])+n]++;
    sum[p+1]=0;
    dwn(i,p,1) ans+=L[-(sum[i]=sum[i+1]+A[i])+n];
    printf("%lld\n",ans);
    return 0;
}
View Code

 

posted @ 2015-12-17 13:15  wzj_is_a_juruo  阅读(257)  评论(0编辑  收藏  举报