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

被折叠的 条评论
为什么被折叠?



