zoukankan      html  css  js  c++  java
  • BZOJ 1303: [CQOI2009]中位数图 【水题】

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

    }

  • 相关阅读:
    Bootstrap导航组件
    Bootstrap输入框组
    Bootstrap按钮式下拉菜单
    Bootstrap按钮组
    Bootstrap下拉菜单
    Bootstrap 中的 aria-label 和 aria-labelledby
    js 在函数中遇到的this指向问题
    js中 clientWidth offsetWidth scrollWidth等区别
    小程序--授权封装
    小程序--分享功能
  • 原文地址:https://www.cnblogs.com/philippica/p/4097699.html
Copyright © 2011-2022 走看看