[Bzoj 1303] [CQOI2009] Median figure "0.0"

Source: Internet
Author: User

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"

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.