Title Link: BZOJ-1303
Problem analysis
First, locate position Pos for B, and then assign the value of less than B in the sequence to-1, and the value greater than B is assigned to 1.
Expand from B to the left, keep counting Sum[i, b-1], and then add one to Cnt[sum[i, b-1], so that the Sum of each left is the number of values.
then expand from B to the right, counting Sum[b + 1, i], then Ans + = cnt[sum[b + 1, I]].
Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> using namespace std; const int MAXN = 100000 + 5; int n, b, num, Pos, Sum;int A[MAXN], CNTL[MAXN * 2]; typedef long Long LL; LL Ans; int main () { scanf ("%d%d", &n, &b); for (int i = 1; I <= n; ++i) { scanf ("%d", &num); if (num = = b) { a[i] = 0; Pos = i; Continue; } if (num < b) A[i] =-1; else a[i] = 1; } Sum = 0; for (int i = Pos; I >= 1; i.) { Sum + = A[i]; ++cntl[sum + n]; } Sum = 0; Ans = 0LL; for (int i = Pos; I <= n; ++i) { Sum + = A[i]; Ans + = (LL) cntl[-sum + n]; } printf ("%lld\n", Ans); return 0;}
[Bzoj 1303] [CQOI2009] Median figure "0.0"