1. 程式人生 > >中位數圖 HYSBZ

中位數圖 HYSBZ

給出1~n的一個排列,統計該排列有多少個長度為奇數的連續子序列的中位數是b。中位數是指把所有元素從小到大排列後,位於中間的數。 Input 第一行為兩個正整數n和b ,第二行為1~n 的排列。 Output 輸出一個整數,即中位數為b的連續子序列個數。 Sample Input 7 4 5 7 2 4 3 1 6 Sample Output 4 Hint

第三個樣例解釋:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000

map 對映就好

#include<iostream>
#include<cstdio>
#include
<algorithm>
#include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional>
#include<sstream> #include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f #define INF 0x7fffffff #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) typedef long long ll; typedef unsigned long long ull; typedef unsigned
int U; #define ms(x) memset((x),0,sizeof(x)) const int mod = 1e9 + 7; #define Mod 20100403 #define sq(x) (x)*(x) #define eps 1e-10 const int N = 1505; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } int n, b; int a[maxn]; map<int, int>Map; int main() { //ios::sync_with_stdio(false); rdint(n); rdint(b); int pos; for (int i = 1; i <= n; i++) { rdint(a[i]); if (a[i] == b)pos = i; } int cnt = 0; for (int i = pos; i <= n; i++) { if (a[i] > b)cnt++; if (a[i] < b)cnt--; Map[cnt]++; } cnt = 0; ll sum = 0; for (int i = pos; i >= 1; i--) { if (a[i] > b)cnt++; if (a[i] < b)cnt--; sum += (ll)Map[0 - cnt]; } cout << sum << endl; return 0; }