• BestCoder Sequence


    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
     
    题解:Bestcoder Sequences显然有个性质,如果令大于m的数为1,令小于m的数为-1,则( a[1]+a[2]+···+a[n] )=0;
    例子:
    5 3
    4 5 3 2 1 <====> 1 1 0 -1 -1 ==>{3},{5 3 2},{4 5 3 2 1}其对应的和都为0。然后再将序列分为两部分,m的左
    边和m的右边。如{4 5}和{2 1},计算它们对应的序列的和,相反的一定能构成Bestcoder Sequences。具体看代码!因
    为和存在是负数的情况,所以这里maxn相当于数轴上的0点。
     1 #pragma warning(disable:4996)
     2 #include<string>
     3 #include<cstdio>
     4 #include<bitset>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 const int maxn = 4e4 + 5;
    12 
    13 int n, m;
    14 int a[maxn], ans[2][2 * maxn];
    15 
    16 int main()
    17 {
    18     while (scanf("%d%d", &n, &m) != EOF) {
    19 
    20         memset(ans, 0, sizeof(ans));
    21         ans[0][maxn] = ans[1][maxn] = 1;
    22 
    23         int p, k;
    24         for (int i = 1; i <= n; i++) {
    25             scanf("%d", a + i);
    26             if (a[i] == m) p = i;
    27         }
    28 
    29         k = 0;
    30         for (int i = p + 1; i <= n; i++) {
    31             if (a[i] > m) k++;
    32             else k--;
    33             ans[1][k + maxn]++;
    34         }
    35 
    36         k = 0;
    37         for (int i = p - 1; i >= 1; i--) {
    38             if (a[i] > m) k++;
    39             else k--;
    40             ans[0][k + maxn]++;
    41         }
    42 
    43         k = 0;
    44         for (int i = -n; i <= n; i++) k += (ans[0][i + maxn] * ans[1][maxn - i]);
    45         printf("%d
    ", k);
    46 
    47     }
    48     return 0;
    49 }
     
  • 相关阅读:
    提升10倍生产力:IDEA远程一键部署SpringBoot到Docker
    如何写出让同事无法维护的代码?
    Nginx 极简教程(快速入门)
    实战SpringCloud响应式微服务系列教程(第一章)
    单节点nginx为两台apache服务器提供负载均衡
    监控Linux性能的18个命令行工具
    Nginx/LVS/HAProxy负载均衡软件的优缺点详解(转)
    LVS包转发模型和调度算法(转)
    借助LVS+Keepalived通过DR模式实现负载均衡
    CentOS 7.0 安装中文输入法
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8612723.html
Copyright © 2020-2023  润新知