[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 ...
随机推荐
- django自带的orm之查询
一.filter条件查询 用法: 模型类.objects.filter(模型类属性名__查询操作符 = 值) 判等: exact # 例:查询id为1的员工 select * from employe ...
- .net开发CAD2008无法调试的解决方法
把acad.exe.config文件修改为:------------------------------------------------------------------------------ ...
- python 机器学习框架scikit-learn安装
1.windows环境whl包下载地址 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.安装numpy.scipy.scikit-learn.matplotl ...
- 曼哈顿距离、欧几里得距离、闵氏距离(p→∞为切比雪夫距离)
曼哈顿距离: 是由十九世纪的赫尔曼·闵可夫斯基所创词汇 ,是种使用在几何度量空间的几何学用语,用以标明两个点在标准坐标系上的绝对轴距总和. 曼哈顿距离——两点在南北方向上的距离加上在东西方向上的距离, ...
- B - ACM小组的古怪象棋 【地图型BFS+特殊方向】
ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...
- 洛谷——P1589 泥泞路
P1589 泥泞路 题目描述 暴雨过后,FJ的农场到镇上的公路上有一些泥泞路,他有若干块长度为L的木板可以铺在这些泥泞路上,问他至少需要多少块木板,才能把所有的泥泞路覆盖住. 输入输出格式 输入格式: ...
- Exchange2010启用反垃圾邮件功能
今天邮箱服务器发现有大量发件人为空的邮件等待执行,也就是说空邮件堵塞了队列. 一般来说,空邮件就是别人发送垃圾邮件给你,你的服务上不存在这个收件人,那么系统会产生一封退信告诉你这封邮件已经被退.而ex ...
- 再谈EditText只能输入金额
上次写了一篇EditText只能输入金额的博客,后来发现一个bug,当还未输入数字的情况下输入小数点程序就崩了,我去测了一下支付宝,看看会怎么样,我先输入小数点,程序正常,我再输入数字,可以正常输入, ...
- MySQL查询时区分大小写(转)
说明:在MySQL查询时要区分大小写会涉及到两个概念character set和collation,这两个概念在表设计时或者在查询时都可以指定的,详细参考:http://www.cnblogs.com ...
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
通常Inno生成的安装文件的图标是一个光盘和显示器,如下图.同时,程序安装好之后,在安装目录下的卸载程序的图标也是一样的,其实我们也可以自己修改. 首先生成的安装文件图标.这个比较简单,只需要在Set ...