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. POJ 2299 Ultra-QuickSort 简单题解

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 68874   Accepted: 25813 ...

  2. String java问题随笔

    1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实 ...

  3. 时间转换,django的时间设置,re模块简单校验密码和手机号

    时间转换和密码,手机的re模块简单校验 import re,time def check_userinfo(request): pwd = request.POST.get("pwd&quo ...

  4. 水题:HDU1303-Doubles

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

  5. 笔记-python-内存管理

    笔记-python-内存管理 1.      内存使用 1.1.    对象的内存使用 a = 1 1是一个对象,a是引用,指向1. >>> id(a) 1951821280 这个数 ...

  6. Unable to execute dex: Multiple dex files define Lcom/myapp/R$array;

    Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; 我这个问题最后解决方式是,吧工程里面用同一个v4包. 很明显, ...

  7. java流、文件以及IO

    读写文件 一个流被定义为一个数据序列.输入流用于从源读取数据,输出流用于向目标写数据. 输入流和输出流的类层次图. FileInputStream FileInputStream用于从文件中读取数据, ...

  8. day 17 jQuery

    什么是jQuery? 可以把它认为是python中的模块,导入就可以使用模块中的功能. jQuery 的版本: 1.xx 系列 2.xx 系列 3.xx 系列 最常用的为1 系列,1系列最新版为1.1 ...

  9. Spider_Man_6 の Scrapy(未完待续)

    一:自我介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所 ...

  10. Mac 如何删除应用、软件

    首先需要跟刚接触Mac的小白分享一下卸载软件常用的两种方法: 1.点击Finder(访达)—应用程序—选择所要删除的软件—拖拽到右下方的废纸篓或者单击右键选择“移除到废纸篓”. 2.打开Launchp ...