您的位置:首页 > 其它

HDU 5701 中位数计数

2016-08-02 16:35 253 查看
中位数计数

Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1230 Accepted Submission(s): 459

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)

思路:

对于 每个数,向前向后找,遇到大的+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
++;
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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: