题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908

BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 618    Accepted Submission(s): 214

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 asBestcoder Sequence.



As the best coder, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which arebestcoder 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 theBestcoder Sequences.
 
Sample Input
1 1
1
5 3
4 5 3 2 1
 
Sample Output
1
3
Hint
For the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
 
Source
 
Recommend
We have carefully selected several similar problems for you:  

pid=4910">4910 4909 4906 

pid=4904">4904 4903 


题意:给定N和M,N为序列的长度。由1~N组成。求有多少连续的子序列是以M为中位数,且长度为奇数。

代码例如以下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define mid 40000
#define MAXN 100017
int dp[MAXN], num[MAXN];
void init()
{
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
}
int main()
{
int n, m;
int i, j;
while(~scanf("%d%d",&n,&m))
{
init();
int t = 0;
for(i = 1; i <= n; i++)
{
scanf("%d",&num[i]);
if(num[i] == m)//记录m位置
t = i;
}
int cont = 0;
for(i = t+1; i <= n; i++)
{
if(num[i] > m)//大的加
cont++;
else //小的减
cont--;
dp[cont+mid]++;//记录出现该状态的次数
}
cont = ++dp[mid];//当状态数为mid。才满足中位数
int tt = 0;
for(i = t-1; i >= 1; i--)
{
if(num[i] > m)
tt++;
else
tt--;
cont+=dp[-tt+mid];//状态相加为mid的个数
}
printf("%d\n",cont);
}
return 0;
}

再贴一张和上面思路同样但做法不同的代码(系转载

将大于M的数标记为1,小于M的数标记为-1。M本身标记

为0,则题目就是要求和为0而且包含M的连续序列的个数。

用sum_i表示从第一个数到第i个数的标记的和。对于全部大

于等于M的位置的i,我们要求小于M的位置的sum_j

== sum_i的个数的和即为答案。

代码例如以下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 50000
using namespace std;
int num[MAXN+10],sum[MAXN+10],a[MAXN+10+MAXN];
int main()
{
int M,N,M_id;
while (scanf("%d %d",&N,&M)!=EOF)
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
num[0]=sum[0]=0;
for (int i=1;i<=N;i++)
{
int tmp;
scanf("%d",&tmp);
if (tmp>M) num[i]=1;
else if (tmp==M) num[i]=0,M_id=i;
else num[i]=-1;
sum[i]=sum[i-1]+num[i];
}
int cnt=0;
for (int j=0;j<=M_id-1;j++)
a[sum[j]+MAXN]++;
for (int i=M_id;i<=N;i++)
cnt+=a[sum[i]+MAXN];
printf("%d\n",cnt);
}
return 0;
}

hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)的更多相关文章

  1. [BestCoder Round#26] Apple 【组合数学】

    题目链接:HDOJ - 5160 题目分析 第一眼看上去,要求统计所有不同排列对答案的贡献.嗯...完全没有想法. 但是,如果我们对每个数字单独考虑,计算这个数字在总答案中的贡献,就容易多了. 对于一 ...

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

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

  3. BestCoder Round #3 A,B

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

  4. BestCoder Round #11 (Div. 2) 题解

    HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  6. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

  7. BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

    BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这 ...

  8. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  9. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. 《算法导论》— Chapter 6 堆排序

    序 本文主要介绍堆排序算法(HeapSort),堆排序像合并排序而不像插入排序,堆排序的运行时间为O(nlgn):像插入排序而不像合并排序,它是一种原地(in place)排序算法.在任何时候,数组中 ...

  2. Task(TPL)简单的实现Winform(WPF)异步

    很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...

  3. window查看哪些端口被占用命令

    管理员方式运行cmd netstat -n

  4. XTUOJ 15503 - C

    15503 - C Accepted: 6    Submissions: 27    Time Limit: 3000 ms    Memory Limit: 1048576 KB 在解决了小女孩的 ...

  5. Leetcode 299.猜字游戏

    猜字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls&q ...

  6. sql通配符+sql中查询条件包含下划线等通配符的写法

    一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...

  7. SPOJ LCS 后缀自动机找最大公共子串

    这里用第一个字符串构建完成后缀自动机以后 不断用第二个字符串从左往右沿着后缀自动机往前走,如能找到,那么当前匹配配数加1 如果找不到,那么就不断沿着后缀树不断往前找到所能匹配到当前字符的最大长度,然后 ...

  8. 【BZOJ3939】Cow Hopscotch(动态开点线段树)

    题意: 就像人类喜欢跳格子游戏一样,FJ的奶牛们发明了一种新的跳格子游戏.虽然这种接近一吨的笨拙的动物玩跳格子游戏几乎总是不愉快地结束,但是这并没有阻止奶牛们在每天下午参加跳格子游戏  游戏在一个R* ...

  9. Android服务Service

    安卓Service服务 一    Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...

  10. Linux下出现launch failed.Binary not found的解决方案

    Linux下出现launch failed.Binary not found的解决方案: Project->Properties->C/C++Build->Settings-> ...