zoukankan      html  css  js  c++  java
  • HDU 4908 BestCoder Sequence(BestCoder Round #3)

    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.
     
    题意:有一个1~n的序列,现在我们要找到这个序列以m为中位数(eg:1 2 3 4 5 || 5 4 3 2 1,这是两个以3为中位数的序列)的连续子序列有多少个。
     
    这里先来看一下1 2 3 4 5这个序列,首先我们可以得到它的sum值(请结合下面的代码来看):
    a[i]         1      2      3      4      5
    sum[i]    -1    -2     -2     -1     0
    由于我们下标都是从1开始的,那么我们可以加上a[0]和sum[0],变成
    a[i]         0      1      2      3      4      5
    sum[i]    0      -1    -2     -2     -1     0
    离散化之后(变成正数,便于模拟,这里假如加上的是40000)
    a[i]                         0             1           2           3            4          5
    sum[i]                    0            -1          -2          -2          -1          0
    sum[i]+40000     40000     39999    39998    39998    39999    40000                         
    首先从0~x-1遍历,这时b[40000] = 1, b[39999] =1, b[39998]=1;那我们在遍历x~n时由上面分析得也会遇见这三个值,那么我们最终就会发现满足条件的序列就是三个。(注意b数组的下标是sump[i]+40000)
     
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=1e6+10;
    const int M=50000;
    const int INF=0x3f3f3f3f;
    int a[N], sum[N], b[N]; 
    
    int main ()
    {
        int n, m, i, x, ans;
    
        while (scanf("%d%d", &n, &m) != EOF)
        {
            memset(sum, 0, sizeof(sum));
            memset(b, 0, sizeof(b));
            ans = 0;
    
            for (i = 1; i <= n; i++)
            {
                scanf("%d", &a[i]);
                
                sum[i] = sum[i-1];
                if (a[i] < m) sum[i]--; ///比中位数小--
                if (a[i] > m) sum[i]++; ///比中位数大++
                
                if (a[i] == m)
                    x = i;
            }
    
            for (i = 0; i < x; i++)
                b[sum[i]+M]++; ///由于我们的sum值可能是负数,那么我们需要将所有的数变成正数,为了结果不受影响,加上一个比较大的数
    
            for (i = x; i <= n; i++)
                ans += b[sum[i]+M]; ///首先我们要明白当两个位置的sum值相等时,那么这中间就会产生一个符合题意的序列
    
            printf("%d
    ", ans);
        }
    
        return 0;
    }
  • 相关阅读:
    C# 连接 Socks5 代理
    使用EF Core 连接远程oracle 不需要安装oracle客户端方法
    PLSQL Developer 12 汉化包下载
    对象异步赋值bug记录
    图片缩放
    根据对象的属性去重,获取新数组
    小程序购物车抛物线(贝塞尔曲线实现)
    文字溢出,换行方法整理
    整理一些最近开发小程序的经验
    【莫傷曉_开发笔记】linux java绘图字体乱码问题
  • 原文地址:https://www.cnblogs.com/syhandll/p/4897747.html
Copyright © 2011-2022 走看看