HDU4908——BestCoder Sequence(BestCoder Round #3)

BestCoder Sequence

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.
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
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
3

题目大意:

    定义了一种序列,满足:1)有奇数个个数 2)中位数为M

    输入一个任意一个数串,输出满足条件的子串个数。

结题思路:

    一开始读错题了。后来看了结题报告才过的。

    附:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #define MAXN  50000
 6 using namespace std;
 7 int num[MAXN+10],sum[MAXN+10],a[MAXN+10+MAXN];
 8 int main()
 9 {
10     int M,N,M_id;
11     while (scanf("%d %d",&N,&M)!=EOF)
12     {
13         memset(a,0,sizeof(a));
14         memset(sum,0,sizeof(sum));
15         memset(num,0,sizeof(num));
16         num[0]=sum[0]=0;
17         for (int i=1;i<=N;i++)
18         {
19             int tmp;
20             scanf("%d",&tmp);
21             if (tmp>M) num[i]=1;
22             else if (tmp==M) num[i]=0,M_id=i;
23             else num[i]=-1;
24             sum[i]=sum[i-1]+num[i];
25         }
26         int cnt=0;
27         for (int j=0;j<=M_id-1;j++)
28             a[sum[j]+MAXN]++;
29         for (int i=M_id;i<=N;i++)
30             cnt+=a[sum[i]+MAXN];
31         printf("%d
",cnt);
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/Enumz/p/3889593.html