zoukankan      html  css  js  c++  java
  • 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
    ", ans+1);
        return 0;
    }
    View Code
  • 相关阅读:
    皇帝的用人之道,这一点古今皆同
    sharepoint打包
    powershellbegin
    taxonomy
    powershelluninstall webapplication
    面试题
    字符串处理
    在页面中插入视频时的文件夹命名问题
    process object
    扩展名显示与隐藏
  • 原文地址:https://www.cnblogs.com/Yokel062/p/11476946.html
Copyright © 2011-2022 走看看