您的位置:首页 > 其它

HDU-5701-中位数计数

2016-05-23 19:08 344 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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

 

Source
2016"百度之星" - 初赛(Astar Round2B)

 

 

 

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8007;
int a[MAXN],cnt[MAXN<<1];
int main()
{
int n,ans;
while(scanf("%d",&n)!=EOF)
{
char s=' ';
for(int i=1; i<=n; ++i)
scanf("%d",&a[i]);
for(int i=1; i<=n; ++i)
{
memset(cnt,0,sizeof(cnt));
int poi=0;cnt
=1;
for(int j=i-1; j>0; --j)
{
if(a[j]<a[i])poi++;
else poi--;
cnt[poi+n]++;
}
poi=0;
ans=cnt
;
for(int j=i+1; j<=n; ++j)
{
if(a[j]>a[i])poi++;
else poi--;
ans+=cnt[poi+n];
}
if(i==n)s='\n';
printf("%d%c",ans,s);
}
}
return 0;
}
/*
5
1 4 3 2 5
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: