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 Map及Map.Entry详解(转)

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...

  2. Lucene学习笔记: 五,Lucene搜索过程解析

    一.Lucene搜索过程总论 搜索的过程总的来说就是将词典及倒排表信息从索引中读出来,根据用户输入的查询语句合并倒排表,得到结果文档集并对文档进行打分的过程. 其可用如下图示: 总共包括以下几个过程: ...

  3. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

  4. Multiple reportviewers on one page With reportviwer 11.0

    Hi,  evreryone: When I use  VS 2012  to create report with reportviwer 11.0, I  meet a  problem abou ...

  5. 从Count看Oracle执行计划的选择

    一. 前言 在调查一个性能问题的时候,一个同事问道,为什么数据库有些时候这么不聪明,明明表上有索引,但是在执行一个简单的count的时候居然全表扫描了!难道不知道走索引更快么? 试图从最简单的coun ...

  6. HttpComponents 学习的两个重要文档

    httpcore-tutorial-simplified-chinese httpclient-tutorial-simplified-chinese

  7. jpa仓库接口

    可以使用的仓库接口有: Repository: 是 Spring Data的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法. CrudRepository: 继承Repos ...

  8. jQuery基础学习1

    标准HTML文件引入jQuery库方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  9. hdu4614Vases and Flowers(线段树,段设置,更新时范围的右边值为变量)

    Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases ...

  10. ssi整合报错org.apache.struts2.convention.ConventionsServiceImpl.determineResultPath(ConventionsServiceImpl.java:100)

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...