HDU4908 (5/600)

本文介绍了一个有趣的编程挑战:计算给定排列中连续子序列作为“最佳编码序列”的数量。最佳编码序列定义为长度为奇数且中位数为特定值M的序列。文章通过示例解释了问题,并提供了一种使用映射来跟踪序列左侧和右侧数值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

TechnicalSpecificationTechnicalSpecification
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

这个题需要按照要求的那个数的左边和右边各自的和,用map记录以后乘算

#include<bits/stdc++.h>
using namespace std;
#define int long long 
int tu[80001],xiao[80001],da[80001];
int n,m;
main()
{
    while(cin>>n>>m)
    {
        memset(da,0,sizeof(da));
        memset(xiao,0,sizeof(xiao));
        int mzb;
        map<int,int>ymp,zmp;
        for(int a=1;a<=n;a++)
        {
            scanf("%lld",&tu[a]);
            if(tu[a]==m)mzb=a;
        }
        int btx=0,btd=0;
        for(int a=mzb+1;a<=n;a++)
        {
            if(tu[a]>m)btd++;
            else btd--;
            ymp[btd]++;
        }
        btx=0,btd=0;
        for(int a=mzb-1;a>=1;a--)
        {
            if(tu[a]>m)btd++;
            else btd--;
            zmp[btd]++;
        }
        int jg=0;
        jg+=zmp[0]*ymp[0];
        jg+=zmp[0]+ymp[0];
        for(int a=1;a<=n+100;a++)
        {
            jg+=zmp[a]*ymp[-a];
            jg+=zmp[-a]*ymp[a];
        }
        printf("%lld\n",jg+1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值