HDU 5701 中位数计数
2016-05-22 22:53
363 查看
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
++;
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;
}
中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数。
现在有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
++;
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;
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity