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;
}
 

最新文章

  1. yii2的权限管理系统RBAC简单介绍
  2. PAT 1045. 快速排序(25)
  3. S1java基础学习笔记
  4. Animation小问题整理
  5. Android UDP
  6. MongoDB 学习笔记(五)索引
  7. HTML5触摸屏touch事件使用实例1
  8. php 前端获取数据
  9. ThinkPHP - 配置项目结构
  10. 如何解决KEIL 5 编KEIL4同RTX系统的project解决方法
  11. 用django创建一个简单的sns
  12. C语言中的函数、数组与指针
  13. CSS选择器渲染效率
  14. duboo解析的入口
  15. 关于现在IT行业从业者一些建议
  16. SQL查询临时表空间的数据
  17. fiddler抓包url有乱码
  18. webpack4 系列教程(二): 编译 ES6
  19. win10 安装多个版本的jdk,如何切换
  20. VIM 实现tab标签页及分屏,切换命令

热门文章

  1. JavaScript相关图书推荐
  2. 如何用chrome修改js代码,跳过网站等待时间
  3. 三、python高级特性(切片、迭代、列表生成器、生成器)
  4. Gym 100507C Zhenya moves from parents (线段树)
  5. Spring入门(9)-AOP初探
  6. [Xcode使用 - 3] 复制Xcode5.1.1中的项目模板到Xcode6.1
  7. SOURCES的文件格式
  8. poj3259
  9. ecshop支持手机号码登录、邮箱登录
  10. Python beautifulsoup模块