bzoj千题计划175:bzoj1303: [CQOI2009]中位数图

http://www.lydsy.com/JudgeOnline/problem.php?id=1303

 

令c[i]表示前i个数中,比d大的数与比d小的数的差,那么如果c[l]=c[r],则[l+1,r]满足条件

 

#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e7;
int c[N*2],g[N];
void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar();}
}
int main()
{
    
    int n,d,x,now=0,pos;
    long long ans=0;
    bool ok=false;
    read(n); read(d);
    c[n]=1;
    for(int i=1;i<=n;i++)
    {
        read(x);
        if(x<d) now--;
        else if(x>d) now++;
        if(x==d) ok=true,pos=i; 
        if(!ok) c[now+n]++;
        else g[i]=now;
    }
    for(int i=pos;i<=n;i++) ans+=c[g[i]+n];
    printf("%lld",ans);
}

 

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 3125  Solved: 1923
[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

 

posted @ 2017-12-31 10:46  TRTTG  阅读(207)  评论(0编辑  收藏  举报