BZOJ 1303 [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

 

题解:思维题,转化成0, 1, -1,从b的位置往两边算一个前缀和,并记录两边的cnt,可能有负数,+n即可。

#include <bits/stdc++.h>

const int maxn=1e5+5;
int a[maxn], lc[2*maxn], rc[2*maxn];

int main()
{
    int n, b;
    scanf("%d%d", &n, &b);
    int pos, data;
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &data);
        if(data==b) {
            pos=i; a[i]=0;
        }
        else a[i]= data>b? 1:-1;
    }
    int lsum=0, rsum=0;
    for(int i=pos+1; i<=n; i++)
    {
        rsum+=a[i];
        rc[n+rsum]++;
    }
    for(int i=pos-1; i; i--)
    {
        lsum+=a[i];
        lc[n+lsum]++;
    }
    long long ans=0;
    for(int i=-n; i<=n; i++)
    {
        if(i!=0) ans+=1ll*lc[-i+n]*rc[i+n];
        else ans+=lc[-i+n]+rc[i+n]+1ll*lc[-i+n]*rc[i+n];
    }
    printf("%lld\n", ans+1);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Yokel062/p/11476946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值