首页 > 代码库 > hdu 4908 BestCoder Sequence

hdu 4908 BestCoder Sequence

BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 711    Accepted Submission(s): 253


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
Hint
For the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
 

Source
BestCoder Round #3
 


题解及代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <string>
using namespace std;
struct node
{
   int l[40100],z,h[40100];  //l记录小于m的数的个数,h记录大于m的数的个数,
                             //z记录大于m和小于m的数个数相等的个数
                             //当然这些是连续子串大于m和小于m抵消之后的数的个数
}l,r;  //l记录m左边的,r记录m右边的情况
int root[40010];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int p=-1;
        l.z=0;r.z=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&root[i]);
            l.l[i]=0;l.h[i]=0;
            r.l[i]=0;r.h[i]=0;
            if(root[i]==m) p=i;
        }
        int sum=0;
        for(int i=p;i>=1;i--)
        {
            if(root[i]==m) sum+=0;
            else if(root[i]<m) sum-=1;
            else  sum+=1;

            if(sum==0) l.z++;
            else if(sum<0) l.l[-sum]++;
            else l.h[sum]++;
        }
        sum=0;
        for(int i=p;i<=n;i++)
        {
            if(root[i]==m) sum+=0;
            else if(root[i]<m) sum-=1;
            else  sum+=1;

            if(sum==0) r.z++;
            else if(sum<0) r.l[-sum]++;
            else r.h[sum]++;
        }

        int ans=0;
        ans=l.z*r.z;
        for(int i=1;i<=m;i++)
        {
            ans+=(l.l[i]*r.h[i]);
            ans+=(l.h[i]*r.l[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
本题的要求是求出一段1-N的序列中以m为中位数的子序列的个数,要求序列长度为奇数。

因为是中位数,所以大于m和小于m的数各占一半,所以如果存在那么必然长度为奇数,所以
直接考虑怎么构造就可以了。

我们可以想到,大于m的数和小于m的数成为一对可以抵消,那么我们把m的位置先找出来,
然后枚举m位置左边大于m的数有多少,小于m的数有多少(抵消之后),和右边大于m的数有多
少,小于m的数有多少(抵消之后),以及等于0的个数有多少。

那么我们最后把左边大于m的个数与右边小于m的个数对应相乘,右边大于m的个数与左边小于
m的个数对应相乘,以及0的个数相乘,就能得到组后结果。

*转载请注明出处,谢谢。
*/