题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=1303
题目大意:
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。
思路:
找到b在数列中的位置设为start,比b大的赋值为-1,比b小的赋值为1,b赋值为0;
只要满足包含数字b且区间和为0的区间均是b为中位数的区间
那么从start往后扫一下,统计一下和,以及和出现次数。(和为0的区间一定满足)
再从start往前扫一下,如果当前和为x,加上右端和为-x出现次数即可。(如果和为0,还需要++)
1 #include<bits/stdc++.h> 2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf 3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时 4 #define Min(a, b) ((a) < (b) ? (a) : (b)) 5 #define Mem(a) memset(a, 0, sizeof(a)) 6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1)) 7 #define MID(l, r) ((l) + ((r) - (l)) / 2) 8 #define lson ((o)<<1) 9 #define rson ((o)<<1|1) 10 #define Accepted 0 11 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂 12 using namespace std; 13 inline int read() 14 { 15 int x=0,f=1;char ch=getchar(); 16 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 17 while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 18 return x*f; 19 } 20 21 typedef long long ll; 22 const int maxn = 1000000 + 10; 23 const int MOD = 1000000007;//const引用更快,宏定义也更快 24 const int INF = 1e9 + 7; 25 const double eps = 1e-6; 26 int a[maxn]; 27 map<int, int>Map; 28 int main() 29 { 30 int n, t, x, start; 31 scanf("%d%d", &n, &t); 32 for(int i = 1; i <= n; i++) 33 { 34 scanf("%d", &x); 35 if(x == t) 36 { 37 start = i; 38 a[i] = 0; 39 } 40 else if(x > t)a[i] = 1; 41 else a[i] = -1; 42 } 43 for(int i = start + 1; i <= n; i++)a[i] += a[i - 1], Map[a[i]]++; 44 int ans = Map[0] + 1; 45 for(int i = start - 1; i >= 1; i--) 46 { 47 a[i] += a[i + 1]; 48 ans += Map[-a[i]]; 49 if(a[i] == 0)ans++; 50 } 51 cout<<ans<<endl; 52 return Accepted; 53 }