BZOJ 1303: [CQOI2009]中位数图( )

这种题做法应该很多吧...说说我的做法

设b出现位置为pos, 从pos开始向右扫一遍顺便维护( x )(> b 的数的个数 - < b 的数的个数). 然后从pos向左扫一遍, 假设到 t 点, cnt = [ t, pos ) 内> b 的数的个数 - < b 的数的个数, 那 t 点对答案的贡献为cnt * h(-cnt).累加起来就OK了

--------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
   
#define rep(i, n) for(int i = 0; i < n; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define h(x) h[(x) + maxn]
   
using namespace std;
 
const int maxn = 100009;
 
int h[maxn << 1], seq[maxn];
 
int main() {
freopen("test.in", "r", stdin);
clr(h, 0);
int n, b, pos, p = 0;
cin >> n >> b;
rep(i, n) {
scanf("%d", seq + i);
if(b == seq[i]) pos = i;
}
for(int i = pos + 1; i < n; ++i) 
seq[i] > b ? h(++p)++ : h(--p)++;
h(0)++;
long long ans = h(0);
p = 0;
for(int i = pos - 1; i >= 0; i--) {
seq[i] < b ? ++p : --p;
ans += h(p);
}
cout << ans << " ";
return 0;
}

--------------------------------------------------------------------------------------

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 1626  Solved: 1059
[Submit][Status][Discuss]

Description

给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。

Input

第一行为两个正整数n和b ,第二行为1~n 的排列。

Output

输出一个整数,即中位数为b的连续子序列个数。

Sample Input

7 4
5 7 2 4 3 1 6

Sample Output

4

HINT

第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000

Source

原文地址:https://www.cnblogs.com/JSZX11556/p/4665634.html