码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1303: [CQOI2009]中位数图 【水题】

时间:2014-11-14 17:33:12      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:io   os   sp   for   on   bs   amp   ef   nbsp   

给出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

 

思路:记录中间那个数左边右边 比它大和比它小的个数,然后乘法原理搞一下

 

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <iostream>

#include <queue>

#define maxn 500000

#define com 100005

using namespace std;

int righ[maxn],lef[maxn],a[maxn];

int main()

{

    int n,b,idx;

    long long ans=0;

    scanf("%d%d",&n,&b);

    for(int i=1;i<=n;i++)

    {

        scanf("%d",&a[i]);

        if(a[i]==b)idx=i;

    }

    int sum=0;lef[com]=righ[com]=1;

    for(int i=idx-1;i>=1;i--)

    {

        sum+=(a[i]>b)?1:-1;

        lef[sum+com]++;

    }

    sum=0;

    for(int i=idx+1;i<=n;i++)

    {

        sum+=(a[i]<b)?1:-1;

        righ[sum+com]++;

    }

    for(int i=com-n;i<=com+n;i++)ans+=lef[i]*righ[i];

    cout<<ans<<endl;

    return 0;

}

BZOJ 1303: [CQOI2009]中位数图 【水题】

标签:io   os   sp   for   on   bs   amp   ef   nbsp   

原文地址:http://www.cnblogs.com/philippica/p/4097699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!