zoukankan      html  css  js  c++  java
  • 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 }
     
  • 相关阅读:
    Kentico的UIPager的pagesize不工作
    哈啰单车流量问题
    安卓手机无限重启是怎么回事?
    Kentico的翻译功能
    Remote Desktop Free Manager
    访问存储在服务器中的CDR
    保持观察者状态达到跳出或不迷
    高通IPQ4019
    satixnet satellite modem T1000e
    802.11n 中HT20 HT40的区别和信道划分及plus,minus含义
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8612723.html
Copyright © 2011-2022 走看看