BZOJ1303 [CQOI2009]中位数图

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 

 

本文作者:ljh2000   

作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

 

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

 
 
正解:乱搞
解题报告:
  把原数组$change$一下,如果大于$b$则标为$1$,小于则标为$-1$,等于标为$0$。直接记录一下前缀和,如果和前面的某个前缀和相减是$0$则说明可行。注意序列必须跨越$b$,而且记录的前缀和在$b$之后就不应该插入了!
 
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 100011;
int n,k,a[MAXN],tot,root;
int mp1[MAXN*2],mp2[MAXN*2];
LL ans;

inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void work(){
	n=getint(); k=getint(); for(int i=1;i<=n;i++) a[i]=getint();
	for(int i=1;i<=n;i++) if(a[i]>k) a[i]=1; else if(a[i]<k) a[i]=-1; else a[i]=0,root=i/*!!!*/;
	int ZERO=100000; mp2[ZERO]=1; tot=0; 
	for(int i=1;i<=n;i++) {
		tot+=a[i]; 
		if(i&1) {
			if(i>=root) ans+=mp2[ZERO+tot]/*!!!*/;
			else mp1[ZERO+tot]++;
		}
		else {
			if(i>=root) ans+=mp1[ZERO+tot]/*!!!*/;
			else mp2[ZERO+tot]++;
		}
	}
	printf("%lld",ans);
}

int main()
{
    work();
    return 0;
}

  

posted @ 2016-12-31 09:26  ljh_2000  阅读(305)  评论(0编辑  收藏  举报