51Nod1682 中位数计数【中位数】

1682 中位数计数
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题

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

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


Input
第一行一个数n(n<=8000)
第二行n个数,0<=每个数<=10^9
Output
N个数,依次表示第i个数在多少包含其的区间中是中位数。
Input示例
5
1 2 3 4 5
Output示例
1 2 3 2 1



问题链接1682 中位数计数

问题分析:本题与《HDU5701 中位数计数【中位数】》是同一个题,代码拿过来直接使用,参见参考链接。

程序说明:统计比它大的(正)和比它小的(负)数的个数,再进行计算。

题记:(略)

参考链接HDU5701 中位数计数【中位数】


AC的C++程序如下:

/* HDU5701 中位数计数 */  
  
#include <iostream>  
#include <cstring>  
  
using namespace std;  
  
const int MAXN = 8000;  
  
int v[MAXN+1], count[2*(MAXN+1)];  
  
int main()  
{  
    int n, ans, cnt;  
  
    while(cin >> n) {  
        for(int i=1; i<=n; i++)  
            cin >> v[i];  
  
        for(int i=1; i<=n; i++) {  
            memset(count, 0, sizeof(count));  
  
            cnt = 0;  
            count[n]++;  
            for(int j=1; j<i; j++) {  
                if(v[i - j] < v[i])  
                    cnt--;  
                else  
                    cnt++;  
                count[n + cnt]++;  
            }  
  
            cnt = 0;  
            ans = count[n];  
            for(int j=1; i+j<=n; j++) {  
                if(v[i+j] < v[i])  
                    cnt--;  
                else  
                    cnt++;  
                ans += count[n - cnt];  
            }  
            if(i==n)  
                cout << ans << endl;  
            else  
                cout << ans << " ";  
        }  
    }  
  
    return 0;  
} 


原文地址:https://www.cnblogs.com/tigerisland/p/7563730.html