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;
}