[Bestcoder round #3] HDU 4908 bestcoder sequence (count)

Source: Internet
Author: User

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;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.