BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 271    Accepted Submission(s): 112

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.
 

题目意思是  再给出的全排列(1~n每一个数字仅仅出现一次)中,找到一个奇数个的子串,要求子串的中位数(大小排序后正中间的数), 为M;

求这样的子串有多少.

首先要把M这个数左边的串预处理下,  假设 遇到大于M的数ji++,然后记录在data[当前位置的奇偶][当前记录的ji]  ,假设当前位置的数小于M的数,ji--; 由于ji记录在数组里,所以ji 要价格50000 以保证不会出现负数的情况.

然后再处理右边的串,ji 又一次计数,

ans+data[位置奇偶, 假设两个位置奇偶同样,代表这条串有奇数个元素][-ji  加个符号,找到前面处理过的左串中,能够互补的串,达到大于M的数和小于M的数 一样多];

相同加个ji取负数后  相同加个 50000,和前面保存一致

#include<stdio.h>
#include<string.h>
int big(int a)
{
return a+50000;
}
int a[50000],ji,data[2][200000];
int main()
{
int n,m,i,j,wei;
int ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==m)
wei=i;
}
ji=0;
memset(data,0,sizeof(data));
for(i=wei;i>=1;i--)
{
if(a[i]<m)
ji--;
if(a[i]>m)
ji++;
if(i&1)
data[1][big(ji)]++;
else
data[0][big(ji)]++;
}
ji=0;
for(i=wei;i<=n;i++)
{
if(a[i]<m)
ji--;
if(a[i]>m)
ji++;
if(i&1)
ans+=data[1][big(-ji)];
else
ans+=data[0][big(-ji)];
}
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理的更多相关文章

  1. [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

    BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...

  2. hdu 4908 BestCoder Sequence

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

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

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

  4. 中值排序的java实现

    public class MidSort { public static void main(String[] args){ ,,,,,,,,}; midSort(arr,,); for(int i: ...

  5. BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...

  6. 【HDU】4908 (杭电 BC #3 1002题)BestCoder Sequence ——哈希

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

  7. BestCoder Sequence

    hdu  4908  Bestcoder Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, ...

  8. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  9. 学习 opencv---(8)非线性滤波:中值滤波,双边滤波

    正如我们上一篇文章中讲到的,线性滤波可以实现很多种不同的图像变换.然而非线性滤波,如中值滤波器和双边滤波器,有时可以达到更好的实现效果. 邻域算子的其他一些例子还有对 二值图像进行操作的形态学算子,用 ...

随机推荐

  1. shakes hands

    Description On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) ...

  2. jyphon 环境变量配置

    Jyphon 是基于java平台python 的一种实现 官网: http://www.jython.org/ 可以从官网下载 jyphon 安装 下载 jython Installer ,下载之后是 ...

  3. oracle逗号字符串拼接小工具

    oracle逗号字符串拼接小工具 http://www.zui#dai#ma.com/share/1932670249667584.htm 在使用oracle进行数据查询时,常常需要使用到in语句,如 ...

  4. MTD中的nand驱动初步分析---面向u-boot

    之前提到nand驱动的初始化分析,有一个结构体 struct mtd_info始终贯穿这些代码 再来分析一下这个结构体的基本功能,如何初始化,如何使用 一.分析过程 看看结构体的出现和使用方式 第一次 ...

  5. 利用iframe技巧获取訪问者qq

    今天工作时,有个暂时加的好友问我,怎么利用web页面获取訪问者的qq. 曾经没有接触过,感觉到非常好奇,可是工作中脑子非常亢奋,转的快,利用所学的知识迅速想到一条技巧,那就是假想用户在进入我们设定的页 ...

  6. J2EE开发框架搭建(1) - maven搭建多项目

    怎样使用maven搭建多个项目 1. 创建一个maven project 2. 在frame-parent项目上面点击右键,新建Maven Module 3. 完毕之后再建立一个web项目 4. 依照 ...

  7. SetFocus、SetCapture和SetActiveView的区别

    1. SetActiveView是MFC框架内的一个函数, 而不是SDK中的函数, 也就是说SDK中没有ActiveView这个概念, 只有在MFC中才有2. SetFocus是SDK中的函数(当然M ...

  8. dm642在线写EPROM.txt

    void wirteEPROM() { //#include <stdio.h>     unsigned short bufeprom[30],i,val;  FILE *fp;     ...

  9. PPS2013校园招聘笔试题

    转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/11473405 一.简答题 (1)一位老师有2个推理能力很强的学生,他告诉 ...

  10. js弹出对话框,遮罩效果,

    刚刚来到实习单位,我跟着廖哥做项目.然后他分配给我一个小小的任务,实现起来总的效果如下: 然后,但我们单击显示数目这个链接的时候,就会弹出一个又遮罩效果的对话框,如下图: 当我们在对话框中再点击里面的 ...