BZOJ 1303: [CQOI2009]中位数图

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 3680  Solved: 2281
[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

 
[Submit][Status][Discuss]


HOME Back

题解:把输入的数小于k的变为-1,大于k的数变为1,等于k的为0;则题目就变为求能覆盖0的且区间和为0的区间的数量;

用奇数排序即可;

参考代码:

 1 #include<bits/stdc++.h>
 2 #define N 100005
 3 using namespace std;
 4 typedef long long ll;
 5 ll n,b,ans,c[2][2*N];
 6 int main() 
 7 {
 8     cin>>n>>b; 
 9     c[0][n]=1;ans=0;
10     for(ll i=0,a,s=n,isRight=0;i<n;i++)
11     {
12         cin>>a;
13         if(a!=b) s+=a>b?1:-1;
14         c[isRight|=a==b][s]++;
15     }
16     for(ll i=0;i<2*n;i++) ans+=c[0][i]*c[1][i];
17     cout<<ans<<endl;
18     return 0;
19 }
View Code
原文地址:https://www.cnblogs.com/csushl/p/10066455.html