# 11377: 【原1377】序列

### 题目描述

author: yuan 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1377 ﻿

## 样例输入

``````7 4
5 7 2 4 3 1 6
``````

## 样例输出

``````4
``````

## Neight99's solution

``````#include <iostream>

using namespace std;

const int maxN = 1e5 + 100;

int N, B, A[maxN], a[maxN], pos, l[2 * maxN] = {0}, r[2 * maxN] = {0},
left1 = 0, right1 = 0;

int main() {
int sum=0;

cin >> N >> B;

for (int i = 1; i <= N; i++) {
cin >> A[i];
if (A[i] > B) {
a[i] = 1;
} else if (A[i] == B) {
a[i] = 0;
pos = i;
} else {
a[i] = -1;
}
}

left1 = right1 = N;

for (int i = pos; i > 0; i--) {
left1 += a[i];
l[left1]++;
}

for (int i = pos; i <= N; i++) {
right1 += a[i];
r[right1]++;
}

for (int i = 1; i <= 2 * N; i++) {
sum += (l[i] * r[2 * N - i]);
}

cout << sum;

return 0;
}
``````

## yyong119's solution

``````#include <cstdio>
#include <iostream>
#define MAX_N 100010
#define OFFSET 100010
using namespace std;
int n, B, B_pos;
int a[MAX_N];
int left_sum[MAX_N + OFFSET], right_sum[MAX_N + OFFSET];
char ch = getchar(); int res = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return res;
}
int main() {
for (register int i = 0; i < n; ++i) {
if (tmp < B) a[i] = -1;
else if (tmp > B) a[i] = 1;
else B_pos = i;
}
int cur_sum = 0;
for (register int i = B_pos - 1; i >= 0; --i) {
cur_sum += a[i];
++left_sum[OFFSET + cur_sum];
}
cur_sum = 0;
for (register int i = B_pos + 1; i < n; ++i) {
cur_sum += a[i];
++right_sum[OFFSET - cur_sum];
}
long long ans = 1;
for (register int i = 0; i < MAX_N + OFFSET; ++i)
ans += left_sum[i] * right_sum[i];
printf("%lld", ans + left_sum[OFFSET] + right_sum[OFFSET]);
return 0;
}
``````