Fzu sort l

Source: Internet
Author: User

/*************************************** * ****** <Br/> ** description: 1 ~ An arrangement of N (<= 50000), which counts the number of consecutive subsequences with an odd number in length. The median is B. <Br/> median refers to the number in the center after all elements are arranged in ascending order. <Br/> ** sample input: 1 // test cases <br/> 7 4 // n B <br/> 5 7 2 4 3 1 6 <br/> ** sample output: 4 // {4}, {7, 2, 4}, {5, 7, 2, 3}, and {5, 7, 2, 4, 3, 6} <br/> ** algorithm: mathematical + bipartite enumeration <br/> ** analysis: 1.a[ I] = SGN (C [I]-B ), s [I] = A [1] + A [2] +... + A [I] <br/> 2. the original problem is equivalent to finding s [a] = s [B], where 0 <= A <C <= B <= n, c [c] = B <br/> 3. so you can sort 0--c-1 and C--N, and then two points to find the number of the same left and right; <br/> 4. if this time is the same as the previous number, you do not need to repeat it. <br/> ** Note: This is a good question for Binary training! <Br/> ************************************ * *********/<br/> # include <iostream> <br/> # include <stdio. h> <br/> # include <algorithm> <br/> # define maxn 100010 <br/> using namespace STD; </P> <p> INT cases, n, b, I, m, p; <br/> int s [maxn]; <br/> int binary1 (int K) {<br/> int L = p-1, R = n + 1; <br/> while (L + 1 <r) {<br/> int mid = (L + r)/2; <br/> If (s [Mid] <k) L = mid; else r = mid; <br/>}< br/> return l; <br/>}< br/> int binary2 (int K) {<br/> int L = P, R = n + 1; <br/> while (L + 1 <r) {<br/> int mid = (L + r)/2; <br/> If (s [Mid] <= k) L = mid; else r = mid; <br/>}< br/> return R; <br/>}< br/> int main () {<br/> freopen ("B. in "," r ", stdin); <br/> scanf (" % d ", & cases); <br/> while (cases --) {<br/> scanf ("% d", & N, & B); <br/> S [0] = 0; P = 0; <br/> for (I = 1; I <= N; I ++) {<br/> scanf ("% d", & M ); <br/> If (M> B) M = 1; else <br/> If (M <B) M =-1; else <br/> m = 0, P = I; <br/> S [I] = s [I-1] + m; <br/>}</P> <p> sort (S, S + p); <br/> sort (S + P, S + n + 1); </P> <p> int ans = 0, last =-1 <20; <br/> for (I = 0; I <p; I ++) {<br/> If (s [I]> = s [p] & S [I] <= s [N]) {<br/> if (I >=1 & S [I] = s [I-1]) {ans + = last; continue ;} <br/> int k1 = binary1 (s [I]); // meets the requirements of S [k2] <s [I], s [k1 + 1]> = s [I] <br/> int k2 = binary2 (s [I]); // s [k2]> S [I], s [k2-1] <= s [I] <br/> // cout <k1 <"<k2 <Endl; <br/> last = k2-k1-1; <br/> ans + = last; <br/>}< br/> printf ("% d/N", ANS ); <br/>}< br/> return 0; <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.