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. Python学习之eventlet.greenpool

    该模块提供对 greenthread 池的支持. greenthread 池提供了一定数量的备用 greenthread ,有效限制了孵化 greenthread 过多导致的内存不足,当池子中没有足够 ...

  2. CSAPP(1):数字的计算机表示——课后题

    2.65 int even_ones(unsigned x) 要求:return 1 when x contains an even number of 1s; 0 otherwise. 假设int ...

  3. ES 基础

    You Know, for Search 安装es时 , jdk最低版本需要 jdk7 默认端口 : 9200 启动后浏览器访问 : localhost:9200 角色关系对照 elasticsear ...

  4. lipo命令

    工作中,xcode工程遇到一个bug file was built for archive which is not the architecture being linked armv7 找了一些资 ...

  5. COS回应7大质疑

    Apple过于封闭,没啥朋友,这家伙应该比较高傲,曾和Intel,IBM and so on..一起玩过!Google过于开放,狐朋狗友,友人泛滥,殃及ecosystem,弊端已显,祸水将至.COS奉 ...

  6. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  7. WebForm中如何防止页面刷新,后退导致的重复提交

    当用户按下浏览器中的 F5 键刷新当前页面时,对这一过程进行检测所需的操作步骤.页面刷新是浏览器对特定用户操作(按 F5 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...

  8. UVa 1630 Folding (区间DP)

    题意:折叠一个字符串,使得其成为一个尽量短的字符串  例如AAAAAA变成6(A) 而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5( ...

  9. Lua学习笔记(三):函数和闭包

    函数 lua的函数以function关键字开始,后跟函数名称和参数,最后以end结束,我们看一个简单的函数定义: function foo() --do something end function ...

  10. [JS 基础] touchEvent中的changedTouches,targetTouches和touches的区别

    对于移动端开发中 touchEvent(触摸事件) 中changedTouches,targetTouches和touches的区别往往不容易弄清,故特意查询了 MDN相关资料 1. 其中,对 cha ...