Bestcoder SequenceTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 271 Accepted Submission (s): 112
Problem DESCRIPTIONMR Potato is a coder.
Mr Potato is the Bestcoder.
One night, an amazing sequence appeared in his dream. Length of this sequence are odd, the median number is M, and he named this sequence as
Bestcoder Sequence.
As the best coder, Mr potato have 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 was a pair of integers N and M in the first line, and an permutation of 1 to N in the second line.
[Technical specification]
1.1 <= N <= 40000
2.1 <= M <= N
Outputfor each case, you should output the number of consecutive sub-sequences which is the
Bestcoder Sequences.
Sample Input
1 115 34 5 3 2 1
Sample Output
Hintfor the second case, {3},{5,3,2},{4,5,3,2,1} is Bestcoder Sequence.
The title meaning is again given the full array (1~n each number appears only once), to find an odd number of substrings, requires the substring of the median (the size of the number of the middle of the order), m;
Find out how many of these substrings are.
First of all, m this number to the left of the string preprocessing, assuming that the number is greater than M ji++, and then recorded in the data[current position of the parity [current record Ji], assuming that the number of the current position is less than the number of M, ji--; Since Ji is recorded in an array, Ji takes a price of 50000 to ensure that there are no negative cases.
And then deal with the right string, Ji counts again,
ans+data[position parity, assuming that the two position parity, representing the string has an odd number of elements][-ji add a symbol, find the left string before processing, can be complementary to the string, the number is greater than m and less than the number of M as many];
The same Ji plus a negative number after the same plus 50000, and the previous preservation consistent
#include <stdio.h> #include <string.h>int big (int a) {return a+50000;} int A[50000],ji,data[2][200000];int Main () {int N,m,i,j,wei;int ans;while (scanf ("%d%d", &n,&m)!=eof) {ans=0; for (i=1;i<=n;i++) {scanf ("%d", &a[i]), if (a[i]==m) wei=i;} Ji=0;memset (data,0,sizeof (data)); for (i=wei;i>=1;i--) {if (a[i]<m) ji--;if (a[i]>m) ji++;if (i&1) data[1] [Big (JI)]++;elsedata[0][big (JI)]++;} Ji=0;for (i=wei;i<=n;i++) {if (a[i]<m) ji--;if (a[i]>m) ji++;if (i&1) ans+=data[1][big (-ji)];elseans+= Data[0][big (-ji)];} printf ("%d\n", ans);} return 0;}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
HDU 4908 bestcoder Sequence found M median is the number of strings that need to be preprocessed