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为满足题意的子串。
 
  1. #include"iostream"
  2. #include"cstdio"
  3. #include"cstring"
  4. #include"algorithm"
  5. using namespace std;
  6. const int ms=40000;
  7. int sum[ms+1],a[ms+20000];
  8. int n,m;
  9. void solve()
  10. {
  11. memset(sum,0,sizeof(sum));
  12. memset(a,0,sizeof(a));
  13. int x,i,ans=0,id;
  14. for(i=1;i<=n;i++)
  15. {
  16. scanf("%d",&x);
  17. sum[i]=sum[i-1];
  18. if(x==m)
  19. {
  20. id=i;
  21. continue;
  22. }
  23. if(x>m)
  24. sum[i]++;
  25. else
  26. sum[i]--;
  27. }
  28. for(i=0;i<id;i++)
  29. {
  30. a[sum[i]+ms]++;
  31. }
  32. for(i=id;i<=n;i++)
  33. ans+=a[sum[i]+ms];
  34. printf("%d\n",ans);
  35. return ;
  36. }
  37. int main()
  38. {
  39. while(scanf("%d%d",&n,&m)==2)
  40. {
  41. solve();
  42. }
  43. return 0;
  44. }
 

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. 多校1005 HDU5785 Interesting (manacher)

    // 多校1005 HDU5785 Interesting // 题意:给你一个串,求相邻两个回文串左边端点*右边端点的和 // 思路:马拉车算出最长回文半径,求一个前缀和,既得到每个点对答案的贡献. ...

  2. Educational Codeforces Round 14

    A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...

  3. nodejs 5.2.0文档自翻译——Path模块

    模块方法概览 Path path.basename(p[, ext]) path.delimiter path.dirname(p) path.extname(p) path.format(pathO ...

  4. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  5. C++问题-UniqueAppObject.cpp(147): error C3861: “GUXClientInit”: 找不到标识符

    问题经过:在同事的产品上增加新功能,拿来的代码包,用VS打开后,提示某个文件不存在,从项目中移除.CPP.H文件后,提示错误,提示如下:1>UniqueAppObject.cpp(147): e ...

  6. js正则验证手机号

    var regp = /^(\+86|86|)1[3458][0-9]{9}$/; if(str==""){ var flag = checkstatus(obj,"&q ...

  7. HQL和Criteria

    HQL: public boolean doCreate(Dept vo) throws Exception { return this.sessionFactory.getCurrentSessio ...

  8. Android Layout_Gravity和Gravity

    简单来说layout_gravity表示子控件在父容器的位置,gravity表示控件内容在控件内的位置. 上面图片的xml代码 <?xml version="1.0" enc ...

  9. 五,整型变量的读入——scanf函数

    我们先不说变量怎么读入,我们先说说读入是什么?为什么要读入? 先来看一个例子,我现在需要计算两个整数的乘积.先看只用前面学过的内容的实现. #include<stdio.h> int ma ...

  10. android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题

    1.       手机上有一个合并过的联系人 2.       编辑合并后的联系人 3.       手机屏幕会缓慢变暗之后再进入编辑界面. 首先找到contacts源码包下的EditContactA ...