天天看點

HDU 5701 中位數計數

Problem Description

中位數定義為所有值從小到大排序後排在正中間的那個數,如果值有偶數個,通常取最中間的兩個數值的平均數作為中位數。

現在有

n個數,每個數都是獨一無二的,求出每個數在多少個包含其的區間中是中位數。

Input

多組測試資料

第一行一個數

n(n≤8000)

第二行

n個數,

0≤每個數

≤109,

Output

N個數,依次表示第

i個數在多少包含其的區間中是中位數。

Sample Input

5

1 2 3 4 5

Sample Output

1 2 3 2 1

O(n^2)統計,對于每個數字,對于比它大的記一個1,小的記一個-1,然後隻要考慮左右相加為0的有多少即可。

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<bitset>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int n, a[maxn];
int cnt[maxn];

int main()
{
    while (~scanf("%d",&n))
    {
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=2*n;j++) cnt[j]=0;
            int x=0;    cnt[n]++;
            for (int j=1;i-j>0;j++)
            {
                if (a[i-j]<a[i]) x--; else x++;
                cnt[n+x]++;
            }
            x=0;
            int y=cnt[n];
            for (int j=1;i+j<=n;j++)
            {
                if (a[i+j]<a[i]) x--; else x++;
                y+=cnt[n-x];
            }
            printf("%d%s",y,i==n?"\n":" ");
        }
    }
    return 0;
}