zoukankan      html  css  js  c++  java
  • hdu 4908(思路题)

    BestCoder Sequence

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


    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
     
    这道题有加强版--hdu 5400 有兴趣可以做下。
    今天看到给秒了,上次百度之星碰到这种题是懵逼,这种题果然要多刷才会有经验。由于是中位数,所以我们分三种情况讨论。
    1.往左边区间找,当大于M的数等于小于M的数时,M肯定是中位数,计数器+1。
    2.往右边区间找同理。
    3。对于左右两边,我们在往左边计数时弄一个数组记录大于(小于)M的数出现num个的次数为cnt[num],当往右边计数时,碰到大于(小于)M的数有num个时,对应左边有cnt[-num]个序列,计数器+=cnt[-num],由于数组下标不能为负,所以加个大数N。
    #include <stdio.h>
    #include <math.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <vector>
    using namespace std;
    const int N = 40005;
    int a[N];
    int cnt[2*N];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            int id = -1;
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
                if(a[i]==m){
                    id = i;
                }
            }
            memset(cnt,0,sizeof(cnt));
            int ans = 0;
            int num = 0,j=0;
            for(int i=id-1;i>=1;i--){ ///往左计数
                j++;
                if(a[i]<m) num++;
                else num--;
                if(num==0) ans++;
                cnt[N+num]++;
            }
            num = 0,j=0;
            for(int i=id+1;i<=n;i++){
                j++;
                if(a[i]<m) num++;
                else num--;
                if(num==0) ans++;
                ans+=cnt[N-num];
            }
            printf("%d
    ",ans+1);
        }
    }
  • 相关阅读:
    mysql 添加权限和撤销权限的实例(亲测可行)
    svn-经常遇到问题解答办法积累(一)
    一个命令查看mysql的所有配置(原创)
    IOC容器Unity的使用及独立配置文件Unity.Config
    JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
    VSS 的修复和扫描
    js正则获取图片的src属性及正则分割一个字符串
    Mongodb 服务(windows环境下)因被强制关闭,导致服务不能启动的处理办法
    dir结果重定向到剪切板
    property配置
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5628136.html
Copyright © 2011-2022 走看看