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. 微软推荐的130道ASP.NET常见面试题及答案

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  2. SilkTest Q&A 8

    Q72.如何在一个testplan中运行所有的testcase? A72. 1.打开testplan 2.点击Run/Run All Tests菜单,SilkTest开始执行testplan中所有的t ...

  3. alv 列标题

    gs_fieldcat-reptext_ddic才是显示列标题的

  4. vscode编写插件

    vscode编写插件详细过程 前言 之前编写了一个vscode插件用vscode写博客和发布,然后有园友要求写一篇来介绍如何开发一个vscode扩展插件,或者说介绍开发这个插件的过程.然而文章还没有写 ...

  5. 与众不同 windows phone (2) - Control(控件)

    原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...

  6. POJ 3007 Organize Your Train part II

    题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...

  7. Velocity教程-脚本语法详解(转)

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  8. android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用

    android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...

  9. VS2012配置astyle格式化代码

    1.工具->扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本. 2.工具->选项->AStyle Formatter->Edit,填入下面的,点击save,确定. ...

  10. QT实现不规则窗体

    看到网上有很多不规则窗体的实现,效果很酷.于是使用QT也实现了一个,QT的不规则窗体实现非常简单,只需要设置一个mask(遮掩)图片,这个图片的格式可以使用png或bmp格式,我使用了png格式,默认 ...