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. 【zigbee】开启及清除NV_RESTORE信息的方法

    1.NV_RESTORE宏的作用 问:coo和终端都已经组网成功 1.这时将coo断电,又一次上电,组网后终端的短地址是否不变? 2.这时终端断电,又一次上电,组网后终端的短地址是否不变? 3.这时C ...

  2. readline-6.3 之arm平台交叉编译

    近期须要弄个CLI命令接口程序,初步设想是须要支持历史命令翻阅,tab键命令补全这种一个东西.经查阅相关文档,深耕百度一番!(google近期不太正常) 实在恼火.发现readline果真是个好东西, ...

  3. 设计模式之Prototype(c++)

    Prototype模型: 作用: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象----克隆(clone)对象. Prototype模型类图如下: 形象说明:如果客户想配钥匙(Conc ...

  4. python发送各类邮件的主要方法

    更多详见: http://www.w3cschool.cc/python/python-email.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法 ...

  5. 【转】有效修改max open files/ulimit -n

    [转]有效修改max open files/ulimit -n_追梦20121222_新浪博客     [转]有效修改max open files/ulimit -n    (2011-11-18 0 ...

  6. [每日一题] 11gOCP 1z0-052 :2013-08-30 差异的增量备份.....................................................A1

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10669381 正确答案:AD 执行增量备份操作时,首先需要的是增量基本备份(increme ...

  7. VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存

                                                                          VMware vSphere服务器虚拟化实验六 vCente ...

  8. 选择屏幕中的下拉框和dialog中下拉框设计

    REPORT  YTEST014. PARAMETERS: auart LIKE vapma-auart  AS LISTBOX   VISIBLE LENGTH 6. AT SELECTION-SC ...

  9. 基于Adaboost的人脸检测算法

    AdaBoost算法是一种自适应的Boosting算法,基本思想是选取若干弱分类器,组合成强分类器.根据人脸的灰度分布特征,AdaBoost选用了Haar特征[38].AdaBoost分类器的构造过程 ...

  10. CentOS 如何修改mysql 用户root的密码

    源地址:http://blog.sina.com.cn/s/blog_6756f85201019zv7.html 第一步:用帐号登录mysql[root@CentOs5 ~]# mysql -u ro ...