您的位置:首页 > 其它

hdu 5701 中位数计数

2016-05-24 22:39 274 查看

题目链接

给一个数列, 每个数只出现一次。 问对于每个数, 有多少个区间是以它为中位数的。

暴力统计, 对于每个数, 小于它的赋值-1, 大于它的赋值1. 然后暴力统计, 具体看代码。

和bzoj一个题一样啊....

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int l[16005], r[16005], a[8005], ans[8500], b[8005];
int main()
{
int n;
while(cin>>n) {
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
mem(ans);
for(int i = 1; i <= n; i++) {
memcpy(b, a, sizeof(a));
for(int j = 1; j <= n; j++) {
if(i == j) {
b[i] = 0;
continue;
}
if(a[j]>a[i]) {
b[j] = 1;
} else {
b[j] = -1;
}
}
int sum = 0;
mem(l);
mem(r);
l
= r
= 1;
for(int j = i-1; j >= 1; j--) {
sum += b[j];
l[sum+n]++;
}
sum = 0;
for(int j = i+1; j <= n; j++) {
sum += b[j];
r[sum+n]++;
}
for(int j = 0; j < 2*n; j++) {
ans[i] += l[j]*r[2*n-j];
}
}
for(int i = 1; i < n; i++) {
printf("%d ", ans[i]);
}
cout<<ans
<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: