Bestcoder Sequence
Problem descriptionmr 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
Bestcoder Sequence.
As the best coder, Mr Potato has strong curiosity, he wonder the number of consecutive sub-sequences which are
Bestcoder SequencesIn a given permutation of 1 ~ N.
Inputinput 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
Outputfor each case, You shocould output the number of consecutive sub-sequences which are
Bestcoder Sequences.
Sample Input
1 115 34 5 3 2 1
Sample output
13HintFor the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
Sourcebestcoder round #3
Solution:
The question is to give a 1-N arrangement, and then determine a number of M (1 <= m <= N), and ask how many consecutive lengths are odd number subsequences, so that m is the median (M in the subsequence ).
Example
5 3
4 5 3 2 1 n = 5, M = 3
{3}, {5, 3, 2}, {4, 5, 3, 2, 1} are consecutive subsequences that match the question ....
At that time, all the lengths containing M were 0, 1, 2 ....... and then judge whether M is the median. The result is timed out .......
Post a question:
After writing a bunch of words, the csdn layout is too.... paste the picture.
Code:
#include <iostream>#include <stdio.h>#include <string.h>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;}