题目链接: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. 牛客网暑期ACM多校训练营(第四场) J 贪心

    链接: https://www.nowcoder.com/acm/contest/143/J #include<bits/stdc++.h> using namespace std; lo ...

  2. luogu2234 [HNOI2002]营业额统计

    treap水过 #include <iostream> #include <cstdlib> #include <cstdio> using namespace s ...

  3. jenkins在linux环境搭建-公司系统

    1.按照这个搭建的项目https://www.cnblogs.com/zishengY/p/7170656.html 2.配置权限https://blog.csdn.net/mynameissls/a ...

  4. 相应缓存设置HttpCacheability 枚举

      成员名称 说明   NoCache 设置 Cache-Control: no-cache 标头.如果没有字段名,则指令应用于整个请求,且在满足请求前,共享(代理服务器)缓存必须对原始 Web 服务 ...

  5. 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing

    acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...

  6. 实体类与实体DTO类之间的转换

    实体类与实体DTO类之间的转换 实体类与实体DTO类之间的转换 1.通过使用第三方序列化反序列化工具Newtonsoft.Json 2.通过反射实现 3.通过表达式目录树加字典缓存实现 4. 通过表达 ...

  7. MITM Proxy环境搭建

    MITM_Proxy环境搭建 环境要求 系统环境要求: Ubuntu 14.04 x64,CentOS 7 x64以上版本系统(建议使用xubuntu 14.04 x64,稳定硬件要求低) Pytho ...

  8. 收藏CSS经典技巧

    一. CSS字体属性简写规则 一般用CSS设定字体属性是这样做的: font-weight: bold; font- style: italic; font-varient: small-caps;  ...

  9. FusionCharts for Flex 如何更改图表数据

    FusionCharts allows to change chart data and re-render the chart, after it has loaded on the user’s ...

  10. secure上传图片

    1.rz -bye 2.upload as ascii 去掉勾选框