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
 
题解:Bestcoder Sequences显然有个性质,如果令大于m的数为1,令小于m的数为-1,则( a[1]+a[2]+···+a[n] )=0;
例子:
5 3
4 5 3 2 1 <====> 1 1 0 -1 -1 ==>{3},{5 3 2},{4 5 3 2 1}其对应的和都为0。然后再将序列分为两部分,m的左
边和m的右边。如{4 5}和{2 1},计算它们对应的序列的和,相反的一定能构成Bestcoder Sequences。具体看代码!因
为和存在是负数的情况,所以这里maxn相当于数轴上的0点。
 1 #pragma warning(disable:4996)
 2 #include<string>
 3 #include<cstdio>
 4 #include<bitset>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 typedef long long ll;
10 
11 const int maxn = 4e4 + 5;
12 
13 int n, m;
14 int a[maxn], ans[2][2 * maxn];
15 
16 int main()
17 {
18     while (scanf("%d%d", &n, &m) != EOF) {
19 
20         memset(ans, 0, sizeof(ans));
21         ans[0][maxn] = ans[1][maxn] = 1;
22 
23         int p, k;
24         for (int i = 1; i <= n; i++) {
25             scanf("%d", a + i);
26             if (a[i] == m) p = i;
27         }
28 
29         k = 0;
30         for (int i = p + 1; i <= n; i++) {
31             if (a[i] > m) k++;
32             else k--;
33             ans[1][k + maxn]++;
34         }
35 
36         k = 0;
37         for (int i = p - 1; i >= 1; i--) {
38             if (a[i] > m) k++;
39             else k--;
40             ans[0][k + maxn]++;
41         }
42 
43         k = 0;
44         for (int i = -n; i <= n; i++) k += (ans[0][i + maxn] * ans[1][maxn - i]);
45         printf("%d\n", k);
46 
47     }
48     return 0;
49 }

 

 
posted @ 2018-03-20 21:17  天之道,利而不害  阅读(179)  评论(0编辑  收藏  举报