• 中位数


    题目描述

    给出一个长度为NN的非负整数序列A_iAi,对于所有1 ≤ k ≤ (N + 1) / 21k(N+1)/2,输出A_1, A_3, …, A_{2k - 1}A1,A3,,A2k1的中位数。即前1,3,5,…1,3,5,…个数的中位数。

    输入格式

    11行为一个正整数NN,表示了序列长度。

    22行包含NN个非负整数A_i (A_i ≤ 10^9)Ai(Ai109)。

    输出格式

    (N + 1) / 2(N+1)/2行,第ii行为A_1, A_3, …, A_{2k - 1}A1,A3,,A2k1的中位数。

    输入输出样例

    输入 #1
    7
    1 3 5 7 9 11 6
    输出 #1
    1
    3
    5
    6

    说明/提示

    对于20\%20%的数据,N ≤ 100N100;

    对于40\%40%的数据,N ≤ 3000N3000;

    对于100\%100%的数据,N ≤ 100000N100000。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1e5+5;
    int n;
    int a[N],b[N];
    struct Tree
    {
        int l,r,mid;
        int num;
    }tree[N<<2];
    int read()
    {
        char c=getchar();int num=0;
        for(;!isdigit(c);c=getchar());
        for(;isdigit(c);c=getchar())
            num=num*10+c-'0';
        return num;
    }
    void build(int root,int l,int r)
    {
        tree[root].l=l,tree[root].r=r,tree[root].mid=l+r>>1;
        if(l==r)
            return;
        build(root<<1,l,tree[root].mid);
        build(root<<1|1,tree[root].mid+1,r);
    }
    void update(int root,int x)
    {
        ++tree[root].num;
        if(tree[root].l==tree[root].r)
            return;
        if(x<=tree[root].mid)
            update(root<<1,x);
        else
            update(root<<1|1,x);
    }
    int query(int root,int num)
    {
        if(tree[root].l==tree[root].r)
            return tree[root].l;
        if(num<=tree[root<<1].num)
            return(query(root<<1,num));
        else
            return(query(root<<1|1,num-tree[root<<1].num));
    }
    int main()
    {
        n=read();
        for(int i=1;i<=n;++i)
            a[i]=read(),b[i]=a[i];
        sort(b+1,b+n+1);
        int bound=unique(b+1,b+n+1)-b;
        build(1,1,n);
        for(int i=1;i<=n;++i)
        {
            int pos=lower_bound(b+1,b+bound+1,a[i])-b;
            update(1,pos);
            if(i%2)
                printf("%d
    ",b[query(1,i/2+1)]);
        }
        return 0;
    }
  • 相关阅读:
    linux基本命令
    Linux中常用的50个命令
    Selenium2之XPath定位
    Selenium2浏览器启动及配置
    python学习内容.05
    python学习内容.04
    python学习内容.03
    python学习内容.02
    python学习内容.01
    RESTful
  • 原文地址:https://www.cnblogs.com/hrj1/p/11206274.html
Copyright © 2020-2023  润新知