bzoj 1303: [CQOI2009]中位数图

题目链接

给n个数,一个值b, 统计所有以b为中位数的序列的个数。序列长度为奇数。数字在1-n之间, 每个数只出现一次。

如果一个数大于b, 那么将他赋值为1, 小于b赋值为-1, 记录数组中b出现的位置, 为pos。

具体看代码.......好难说清

#include<bits/stdc++.h>
using namespace std;const int maxn = 1e5+;
int a[maxn], l[maxn*], r[maxn*];
int main()
{
int n, b, x, pos;
while(cin>>n>>b) {
for(int i = ; i<n; i++) {
scanf("%d", &x);
if(x == b) {
pos = i;
} else if(x<b) {
a[i] = -;
} else {
a[i] = ;
}
}
int sum = ;
l[n] = r[n] = ;
for(int i = pos-; i>=; i--) {
sum += a[i];
l[sum+n]++; //因为数组不能有负数, 所以+n
}
sum = ;
for(int i = pos+; i<n; i++) {
sum += a[i];
r[sum+n]++;
}
sum = ;
for(int i = ; i<*n; i++) {
sum += l[i]*r[*n-i];
}
cout<<sum<<endl;
}
return ;
}
上一篇:List、Map、Set


下一篇:UvaLive7362 Fare(欧拉函数)