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
Hint
For the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
 
Source

解题思路:

题意为 给定一个1 -N的排列,再给定一个数M(1<=M<=N)。问有多少连续的长度为奇数子序列,使得M在当中为中位数(M在子序列中)。

比方例子

5 3

4 5 3 2 1 N=5, M=3

{3},{5,3,2},{4,5,3,2,1} 为符合题意的连续子序列....

当时做的时候把包括M的全部长度为0,1,2.......的连续子序列都枚举了出来。然后推断推断M是否是中位数。结果
果断超时.......

贴一下题解思路:

写了一堆字,CSDN的排版太....了,贴图片把.

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=40010;
int sum[maxn];
int cnt[maxn*2];
int n,m;
int val,p; int main()
{
while(scanf("%d%d",&n,&m)==2)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1];
scanf("%d",&val);
if(val<m)
sum[i]--;
else if(val>m)
sum[i]++;
else
p=i;
}
memset(cnt,0,sizeof(cnt));
for(int i=0;i<p;i++)
cnt[sum[i]+maxn]++;
int ans=0;
for(int i=p;i<=n;i++)
ans+=cnt[sum[i]+maxn];
printf("%d\n",ans);
}
return 0;
}

[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)的更多相关文章

  1. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  2. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  3. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

  4. hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. hdu 4908 BestCoder Sequence

    # include <stdio.h> # include <algorithm> using namespace std; int main() { int n,m,i,su ...

  6. [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. BestCoder Round #60/HDU 5505 暴力数学

    GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...

  8. BestCoder Round #92 (hdu 6015 6016)

    比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...

  9. hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Jav ...

随机推荐

  1. DRF的认证与权限功能

    认证 1.全局配置 在setting.py进行配置. REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( # 'rest_framework. ...

  2. DRF视图集的使用

    # 原创,转载请留言联系 如果要把同一种http请求方法的多个接口放在同一个视图中,比如:查询多条数据和一条数据都是get请求,放在同一个视图里,应该怎么做呢??? 如果直接放在一起,会怎样呢? # ...

  3. solr params.json

    The Request Parameters API allows creating parameter sets that can override or take the place of par ...

  4. hdu 5185(动态规划)

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. Bringing up interface eth0: Determining if ip address 10.109.67.81 is already in use for device eth0...

    重启网卡出现提示: Bringing up interface eth0:  Determining if ip address 10.109.67.81 is already in use for ...

  6. js处理富文本编辑器转义、去除转义、去除HTML标签

    富文本编辑器生成的HTML标签,进行转义,然后写入数据库,防止脚本注入: function htmlEncode(value){ return $('<div/>').text(value ...

  7. dfs序七个经典问题[转]

    dfs序七个经典问题 参考自:<数据结构漫谈>-许昊然 dfs序是树在dfs先序遍历时的序列,将树形结构转化成序列问题处理. dfs有一个很好的性质:一棵子树所在的位置处于一个连续区间中. ...

  8. 大数模板(Java)

    大数加法 /* 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B ...

  9. POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】

    Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...

  10. HDU 多校1.3

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...