• 2016"百度之星"


    Problem Description

    中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数。

    现在有n个数,每个数都是独一无二的,求出每个数在多少个包含其的区间中是中位数。

    Input

    多组测试数据

    第一行一个数n(n≤8000)

    第二行n个数,0≤每个数≤10^9

    Output

    N个数,依次表示第i个数在多少包含其的区间中是中位数。

    Sample Input

    5

    1 2 3 4 5

    Sample Output

    1 2 3 2 1

    #include<iostream>
    #include<cstdio>
    #include <cstring>
    using namespace std;
    
    int p[20005],n,a[10000];
    int main()
    {
        while (~scanf("%d",&n))
        {
            for (int i=1;i<=n;i++) scanf("%d",&a[i]);
            for (int i=1;i<=n;i++)//从左向右扫描
            {
                memset(p,0,sizeof(p));//p[i]:表示偏离程度为i的次数
                int s=0;
                p[8000]=1;//
                for (int j=i-1;j>=1;j--)
                {
                    if (a[j]<a[i]) s++;//   +---+++-+i--+--++
                    else s--;//
                    p[s+8000]++;//加8000是做预处理,否则下标可能为负
                }
                int S=p[8000];//左边大于和小于个数相等
                s=0;
                for (int j=i+1;j<=n;j++)
                {
                    if (a[j]<a[i]) s++;
                    else s--;
                    S+=p[8000-s];//
                }
                cout<<S;
                if (i==n) cout<<endl;
                else cout<<' ';
            }
        }
        return 0;
    }

     来源:xujt2016

  • 相关阅读:
    以太坊:用 Solidity 写测试用例
    以太坊:测试合约
    以太坊:支持 Quorum 开发
    以太坊:编写外部脚本
    以太坊:使用控制台
    以太坊:调试合约
    Rancher 2.x 搭建及管理 Kubernetes 集群
    我的友情链接
    我的友情链接
    我的友情链接
  • 原文地址:https://www.cnblogs.com/emptyCoder/p/5533331.html
Copyright © 2020-2023  润新知