[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence
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.
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
1 1
1
5 3
4 5 3 2 1
1
3HintFor the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
解题思路:
题意为 给定一个1 -N的排列,再给定一个数M(1<=M<=N)。问有多少连续的长度为奇数子序列,使得M在当中为中位数(M在子序列中)。
比方例子
5 3
4 5 3 2 1 N=5, M=3
{3},{5,3,2},{4,5,3,2,1} 为符合题意的连续子序列....
当时做的时候把包括M的全部长度为0,1,2.......的连续子序列都枚举了出来。然后推断推断M是否是中位数。结果
果断超时.......
贴一下题解思路:
写了一堆字,CSDN的排版太....了,贴图片把.
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=40010;
int sum[maxn];
int cnt[maxn*2];
int n,m;
int val,p; int main()
{
while(scanf("%d%d",&n,&m)==2)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1];
scanf("%d",&val);
if(val<m)
sum[i]--;
else if(val>m)
sum[i]++;
else
p=i;
}
memset(cnt,0,sizeof(cnt));
for(int i=0;i<p;i++)
cnt[sum[i]+maxn]++;
int ans=0;
for(int i=p;i<=n;i++)
ans+=cnt[sum[i]+maxn];
printf("%d\n",ans);
}
return 0;
}
[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)的更多相关文章
- 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 ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理
BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 4908 BestCoder Sequence
# include <stdio.h> # include <algorithm> using namespace std; int main() { int n,m,i,su ...
- [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)
Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- BestCoder Round #60/HDU 5505 暴力数学
GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...
- BestCoder Round #92 (hdu 6015 6016)
比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...
- hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Jav ...
随机推荐
- 部署web应用到虚拟主机的三种方式
方式一: 在 [tomcat]/conf/server.xml 文件中的<Engine>标签下的<Host>标签内部, 添加一个 <Context ...
- ANDROID NFC读M1卡
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Cons ...
- Python_Tips[7] -> 偏函数
偏函数 / Partial Function 使用偏函数可以对函数的部分预先知道的参数进行冻结,从而缓存函数参数,而在运行时再释放参数进行使用.所以偏函数适用于需要多次调用同样一个函数且其中一个参数固 ...
- Retrying Operations using Spring's RetryTemplate
If your application is using Spring then it is easier to use the Spring Framework's RetryTemplate. T ...
- 22、Django实战第22天:课程评论
1.编辑course-comment.html 2.编辑courses.views.py class CourseCommentView(LoginRequiredMixin, View): def ...
- 【C++】const 常引用的用法
用法: const int & a; 定义了一个整型常引用变量a. 特点: 1.不能够通过常引用变量来修改其值,如: const int & a = 10; a = 12;//编译报错 ...
- JavaScript中的map方法
假设有一个函数f(x)=x²;作用在一个数组[1,2,3,4,5,6,7,8,9],通常我们会用遍历的方法来实现. function f(x){ return x * x; } var arr = [ ...
- 【动态规划】Gym - 100923A - Por Costel and Azerah
azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...
- [TC-FindingFriends]Finding Friends
题目大意: 给定一个长度为$n(n\le10^5)$的数列$A(A_i\le10^9)$,求最小的$k$满足存在一个长度至少为$m(m\le n)$的子串,对于串中的每一个数$A_i$,都至少存在一个 ...
- 1.1(java学习笔记) 面向过程与面向对象
面向过程思考时,我们会先思考具体的步骤,第一步走什么,第二步做什么. 比如电脑新建文件夹,第一步:打开电脑 第二步:按下鼠标右键. 第三步:找到新建选项 第四步:点击新建选项下的文件夹 c语言是典型的 ...