HDU 4908 BestCoder Sequence, hdu4908
Mark numbers greater than M as numbers smaller than 1 M as-1 M itself as 0
The question is the number of consecutive sequences that require 0 and contain M.
Sum [I] is used to represent the sum of the numbers marked by the largest number of I numbers in 1st.
For all I at a location greater than or equal to M, We Need To sum the number of sum [j] = sum [I] at a location smaller than M as the answer.
# Include <bits. stdc ++. h> using namespace std; int sum [40020], f [80020]; int main () {int N, M, k,; while (scanf ("% d", & N, & M) = 2) {memset (f, 0, sizeof (f); memset (sum, 0, sizeof (sum); for (int I = 1; I <= N; I ++) {scanf ("% d", & a); if (a> M) sum [I] = sum [I-1] + 1; else if (a <M) sum [I] = sum [I-1]-1; else {k = I; sum [I] = sum [I-1] ;}for (int I = 0; I <= K-1; I ++) f [sum [I] + 40000] ++; int ans = 0; for (int I = k; I <= N; I ++) ans + = f [sum [I] + 40000]; printf ("% d \ n", ans);} return 0 ;}