BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1614    Accepted Submission(s): 566

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
 
这道题有加强版--hdu 5400 有兴趣可以做下。
今天看到给秒了,上次百度之星碰到这种题是懵逼,这种题果然要多刷才会有经验。由于是中位数,所以我们分三种情况讨论。
1.往左边区间找,当大于M的数等于小于M的数时,M肯定是中位数,计数器+1。
2.往右边区间找同理。
3。对于左右两边,我们在往左边计数时弄一个数组记录大于(小于)M的数出现num个的次数为cnt[num],当往右边计数时,碰到大于(小于)M的数有num个时,对应左边有cnt[-num]个序列,计数器+=cnt[-num],由于数组下标不能为负,所以加个大数N。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = ;
int a[N];
int cnt[*N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int id = -;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==m){
id = i;
}
}
memset(cnt,,sizeof(cnt));
int ans = ;
int num = ,j=;
for(int i=id-;i>=;i--){ ///往左计数
j++;
if(a[i]<m) num++;
else num--;
if(num==) ans++;
cnt[N+num]++;
}
num = ,j=;
for(int i=id+;i<=n;i++){
j++;
if(a[i]<m) num++;
else num--;
if(num==) ans++;
ans+=cnt[N-num];
}
printf("%d\n",ans+);
}
}

hdu 4908(思路题)的更多相关文章

  1. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  2. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 4859(思路题)

    Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. hdu 4956(思路题)

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

  7. hdu 5400(思路题)

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. JZOJ 3383. 【NOIP2013模拟】太鼓达人

    3383. [NOIP2013模拟]太鼓达人 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limits ...

  2. 动态规划:HDU2159-FATE(二维费用的背包问题)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. [USACO]Bovine Genomics

    Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...

  4. 访问tomcat出现HTTP Status 500 - java.lang.IllegalStateException: No output folder

    问题:tomcat分为安装版和解压缩版,解压缩版如果解压到安装盘,在浏览器中访问http://localhost:8080,可能会出现500错误,错误提示如下:  localhost:8080 jav ...

  5. Redis实现之字典跳跃表

    跳跃表 跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.跳跃表支持平均O(logN).最坏O(N)的时间复杂度查找,还可以通过顺序性操作来批量处理节 ...

  6. 计算时间复杂度&空间复杂度

    1.下面函数的复杂度是: long foo(long x){ if(x<2) return 1; return x*x*foo(x-1); } 解析: 当n>=2时 foo(n)=n^2* ...

  7. Java开发配置

    http://www.runoob.com/java/java-environment-setup.html

  8. flex遭遇text-overflow:hidden,white-space:nowrap

    最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...

  9. vue2.x生命周期

    vue2.x生命周期 1. beforeCreate 在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用. 2. created 实例已经创建 ...

  10. SPFA - Luogu 3385 【模板】负环

    [模板]负环 描述 找负环 输入 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每行三个整数a b w,表示a->b有一条权值为w ...