1. 程式人生 > >CF1005E1 Median on Segments (Permutations Edition) 思維

CF1005E1 Median on Segments (Permutations Edition) 思維

and tar elements fine contains middle 題意 ati row

Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output

You are given a permutation p1,p2,,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.

Find the number of pairs of indices (l,r)(l,r) (1lrn1≤l≤r≤n) such that the value of the median of pl,pl+1,,prpl,pl+1,…,pr is exactly the given number mm.

The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.

Write a program to find the number of pairs of indices (l,r)(l,r) (1lrn1≤l≤r≤n) such that the value of the median of pl,pl+1,,prpl,pl+1,…,pr is exactly the given number mm.

Input

The first line contains integers nn and mm (1n2?1051≤n≤2?105, 1mn1≤m≤n) — the length of the given sequence and the required value of the median.

The second line contains a permutation p1,p2,,pnp1,p2,…,pn (1pin1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.

Output

Print the required number.

Examples input Copy
5 4
2 4 5 3 1
output Copy
4
input Copy
5 5
1 2 3 4 5
output Copy
1
input Copy
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
output Copy
48
Note

In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).

題意:給你n個數和m,問在這n個數中以m為中位數的區間有多少個?

因為要使m為中位數,肯定是m的值位於這些數的中間的部分,即要有比m大的數和比m小的數,且大的數和小的數要相等或者大的數比小的數多一

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll a[maxn], vis[maxn];
int main() {
    ll n, m;
    while( cin >> n >> m ) {
        map<ll,ll> mm;
        ll  pos;
        for( ll i = 1; i <= n; i ++ ) {
            cin >> a[i];
            if( a[i] == m ) {
                pos = i;
            }
        }
        ll cnt = 0;
        for( ll i = pos; i <= n; i ++ ) {
            if( a[i] > m ) {
                cnt ++;
            } else if( a[i] < m ) {
                cnt --;
            }
            mm[cnt] ++;
        }
        ll ans = 0;
        cnt = 0;
        for( ll i = pos; i >= 1; i -- ) {
            if( a[i] > m ) {
                cnt ++;
            } else if( a[i] < m ) {
                cnt --;
            }
            ans += mm[-cnt];
            ans += mm[1-cnt];  //個數為偶數,中位數在中間兩位的左邊一位
        }
        cout << ans << endl;
    }
    return 0;
}

CF1005E1 Median on Segments (Permutations Edition) 思維