BestCoder Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2140 Accepted Submission(s): 703
Total Submission(s): 2140 Accepted Submission(s): 703
Problem Description
Mr Potato is a coder.
Mr Potato is the BestCoder.
One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence.
As the best coder, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which are bestcoder sequences in a given permutation of 1 ~ N.
Mr Potato is the BestCoder.
One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence.
As the best coder, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which are bestcoder sequences in a given permutation of 1 ~ N.
Input
Input contains multiple test cases.
For each test case, there is a pair of integers N and M in the first line, and an permutation of 1 ~ N in the second line.
[Technical Specification]
1. 1 <= N <= 40000
2. 1 <= M <= N
For each test case, there is a pair of integers N and M in the first line, and an permutation of 1 ~ N in the second line.
[Technical Specification]
1. 1 <= N <= 40000
2. 1 <= M <= N
Output
For each case, you should output the number of consecutive sub-sequences which are the Bestcoder Sequences.
Sample Input
1 1 1 5 3 4 5 3 2 1
Sample Output
1 3HintFor the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
题意:
给一个序列,里面是1~N的排列,给出M,问以M为中位数的奇数长度的序列个数。
分析:
就是记录M左右两边区间比M大的和比M小的差值的个数,再相乘。
可以先遍历左边记录差值个数,右边遍历直接加上左边的一样的差值
给一个序列,里面是1~N的排列,给出M,问以M为中位数的奇数长度的序列个数。
分析:
就是记录M左右两边区间比M大的和比M小的差值的个数,再相乘。
可以先遍历左边记录差值个数,右边遍历直接加上左边的一样的差值
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
using namespace std;
const int maxn = 40010;
int sum[maxn];
int cnt[maxn * 2];
int n, m;
int val, p;
int main()
{
while (scanf("%d%d", &n, &m) == 2)
{
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
sum[i] = sum[i - 1];
scanf("%d", &val);
if (val < m)
{
sum[i]--;
}
else if (val > m)
{
sum[i]++;
}
else
{
p = i;
}
}
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < p; i++)
{
cnt[sum[i] + maxn]++;
}
int ans = 0;
for (int i = p; i <= n; i++)
{
ans += cnt[sum[i] + maxn];
}
printf("%d\n", ans);
}
return 0;
}