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。
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <string.h>
  6. #include <vector>
  7. using namespace std;
  8. const int N = ;
  9. int a[N];
  10. int cnt[*N];
  11. int main()
  12. {
  13. int n,m;
  14. while(scanf("%d%d",&n,&m)!=EOF){
  15. int id = -;
  16. for(int i=;i<=n;i++){
  17. scanf("%d",&a[i]);
  18. if(a[i]==m){
  19. id = i;
  20. }
  21. }
  22. memset(cnt,,sizeof(cnt));
  23. int ans = ;
  24. int num = ,j=;
  25. for(int i=id-;i>=;i--){ ///往左计数
  26. j++;
  27. if(a[i]<m) num++;
  28. else num--;
  29. if(num==) ans++;
  30. cnt[N+num]++;
  31. }
  32. num = ,j=;
  33. for(int i=id+;i<=n;i++){
  34. j++;
  35. if(a[i]<m) num++;
  36. else num--;
  37. if(num==) ans++;
  38. ans+=cnt[N-num];
  39. }
  40. printf("%d\n",ans+);
  41. }
  42. }

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. Vue之简易的留言板功能

    今天我将带大家通过Vue的todolist案例来完成一个简易的网页留言板! LES'T GO! 首先我们需要在页面上搭建一个input文本输入框,并设置提交按钮,通过循环指令来完成输入框的信息提交! ...

  2. Oracle 数据库密码过期问题

    (1)在CMD命令窗口中输入:           sqlplus 用户名/密码@数据库本地服务名 as sysdba;(如:sqlplus scott/1234@oracle1 as sysdba; ...

  3. bs4的简单应用之防止xss攻击和文本截断

    BeautifulSoup可以过滤html标签,根据这个功能我们可以防止xss攻击和进行文本过滤 1. 安装 pip install beautifulsoup4 2.导入.使用 from bs4 i ...

  4. 华东交通大学2018年ACM“双基”程序设计竞赛 D

      摸鱼之王MIKU酱想去埃及玩,需要一个人陪同.小新和小磊都想陪MIKU酱一起去,但名额只有一个.所以小磊和小新决定用一个小游戏来决定谁和MIKU酱出去玩.    游戏的道具是21张塔罗牌,塔罗牌分 ...

  5. 洛谷P4231 三步必杀

    题目描述: $N$ 个柱子排成一排,一开始每个柱子损伤度为0. 接下来勇仪会进行$M$ 次攻击,每次攻击可以用4个参数$l$ ,$r$ ,$s$ ,$e$ 来描述: 表示这次攻击作用范围为第$l$ 个 ...

  6. UVA_1025 a Spy in the Metro 有向无环图的动态规划问题

    应当认为,有向无环图上的动态规划问题是动态规划的基本模型之一,对于某个模型,如果可以转换为某一有向无环图的最长.最短路径问题,则可以套用动态规划若干方法解决. 原题参见刘汝佳紫薯267页. 在这个题目 ...

  7. Curl之解决中文乱码

    利用iconv命令 curl http://www.baidu.com | iconv -f gb2312 -t utf-8 iconv命令可以将一种已知的字符集文件转换成另一种已知的字符集文件.它的 ...

  8. Android开发——HandlerThread以及IntentService详解

    .HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...

  9. Python爬虫作业

    题目如下:   请分析作业页面(https://edu.cnblogs.com/campus/hbu/Python2018Fall/homework/2420),    爬取已提交作业信息,并生成已提 ...

  10. Visual C++斗地主游戏网络版源代码

    说明:VC++ 6.0写的网络版斗地主游戏,程序基于directx SDK开发,因此你在编译时需要引入相应文件才可以,本斗地主可以支持双人对战.网络对战,游戏随机产生地主,动画发牌,界面也不错,还有声 ...