hdu   4908  Bestcoder

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
 
 
建模好了,很好做。对于满足题意的子串,大于M的个数等于小于M的个数。我们只关心大于小于M这个性质。
我们把大于M的数记作1,小于M的数记作-1,M记作0,则连续的包含M的和为0的子串就是满足题意的子串。
建立模型。我们用数组sum[i]表示1->i  的和。对于大于等于M_ID  的数  i,sum[i],如果sum[j]==sum[i](j<M_id)
则j+1到I为满足题意的子串。
 
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int ms=40000;
int sum[ms+1],a[ms+20000];
int n,m;
void solve()
{
memset(sum,0,sizeof(sum));
memset(a,0,sizeof(a));
int x,i,ans=0,id;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
sum[i]=sum[i-1];
if(x==m)
{
id=i;
continue;
}
if(x>m)
sum[i]++;
else
sum[i]--;
}
for(i=0;i<id;i++)
{
a[sum[i]+ms]++;
}
for(i=id;i<=n;i++)
ans+=a[sum[i]+ms];
printf("%d\n",ans);
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
solve();
}
return 0;
}
 

BestCoder Sequence的更多相关文章

  1. HDU4908——BestCoder Sequence(BestCoder Round #3)

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

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

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

  3. hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理

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

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

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

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

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

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

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

  7. hdu 4908 BestCoder Sequence

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

  8. BestCoder Round #3 A,B

    A.预处理出来,0(1)输出. Task schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. hdu4908(中位数)

    传送门:BestCoder Sequence 题意:给一个序列,里面是1-N的排列,给出m,问以m为中位数的奇数长度的序列个数. 分析:先找出m的位置,再记录左边比m大的状态,记录右边比m大的状态,使 ...

随机推荐

  1. java 复习002

    java东西太多了,我都有点小凌乱了,记得太没结构了 java内存回收机制:垃圾收集GC(Garbage Collection) 两种常用方法: 引用计数(早期使用) 简介:堆中对象每次被栈中引用指向 ...

  2. 第二百六十三天 how can I 坚持

    今天解脱了,放下了,小罗娜,不给力,话说下一步该咋办呢. 鱼不想过双十一自杀了.这都二十二号了,好快. 该把给罗娜说的那些话保存下来.可惜已经删了. 不知道做的对不对,反正就是没缘分,就这样吧. 睡觉 ...

  3. 【转】Maven实战(六)--- dependencies与dependencyManagement的区别

    原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829   感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...

  4. crontab 获取本机ip

    写了个shell获取ip的函数,如下 function GetLocalIP() { ifconfig | grep 'inet '| grep -v '127.0.0.1' | cut -d: -f ...

  5. linux系统日常管理

    笔者在前面介绍的内容都为linux系统基础类的,如果你现在把前面的内容全部很好的掌握了,那最好了.不过笔者要说的是,即使你完全掌握了,你现在还是不能作为一名合格的linux系统管理员的,毕竟系统管理员 ...

  6. POJ 2481 Cows (数组数组求逆序对)

    题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...

  7. error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏的解决方案

    首先,卸载相关的软件,然后删除所有和virtual studio相关的目录 然后如果还出现该问题,则: Mark一下,解决方法:将嵌入清单设置为"否" 发生场合:在用 C++写一个 ...

  8. Weblogic安装

    1.下载wls_121200.jar, 2.输入cnd打开命令提示 3.f:进入F盘 4.Java -version 验证是否配置Java环境, 5.echo %path% 查看环境变量 6.set ...

  9. GUI图形界面

    一.界面元素 1.首层容器:Jwindow.JFrane(窗体).JDialge(对话框).JApplet. 2.组件:标签.文本框.密码框.按钮.下拉列表.单选框.复选框.文本域等 注:标签JLab ...

  10. CESAsia:英特尔RealSense3D实感技术亮眼

    每年CES展会上都会有许多新奇的.更具创意的产品和创新技术亮相,而作为全球科技盛会之一的CES Asia也不例外.在CES Asia2016展会上,英特尔(Intel)可谓是有备而来,带着旗下支持Re ...