码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4908 BestCoder Sequence

时间:2014-08-05 11:22:52      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

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
 


题解及代码:


#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的个数相乘,就能得到组后结果。

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


hdu 4908 BestCoder Sequence,布布扣,bubuko.com

hdu 4908 BestCoder Sequence

标签:des   style   blog   http   color   java   os   io   

原文地址:http://blog.csdn.net/knight_kaka/article/details/38379969

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!