[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 ...
随机推荐
- 《Java编程思想》笔记 第七章 复用类
1.组合 将其他类的对象引用置于新的类中. 3.继承 extends 从已知的一个类中派生出新的一个类,叫子类.子类实现了父类所有 非私有化 非静态 的属性和方法,并能根据自己的实际需求扩展出新的行为 ...
- selenium 3.0鼠标事件 (java代码)
注意:ActionChains下相关方法在当前的firefox不工作,建议使用谷歌浏览器. public static void main(String[] args) throws Interrup ...
- 前端工程师必备PS技能
PS参考线及其辅助 视图-->新建参考线 也可以快捷键Ctrl+R,显示区的上侧和左侧就会出现标尺.任意点选中,并拖动标尺,便会出现浅蓝色的辅助线.只有在移动工具下,才能拖动某一条参考线. 删除 ...
- MATLAB中的积分运算
MATLAB中计算一元函数的(不)定积分使用int函数. ①int(s)计算符号表达式s的不定积分 syms x;s = x^2;int(s) 计算x^2的不定积分. ②int(s,x)计算符号表达式 ...
- Ubuntu 16.04 win7 双系统时间问题
在安装了win7的电脑上又装了一个Ubuntu 16.04,这Ubuntu的启动速度慢就选不说了,切加win7之后发现时间也不对啊. 所以记一个随笔记录一下自己修改双系统的日期. 当然,网上也搜过,说 ...
- python 全局解释锁GIL
Python的全局解释器锁GIL用于保护python解释器,使得任意时刻,只有一个线程在解释器中运行.从而保证线程安全 在多线程环境中,Python 虚拟机按以下方式执行: 1. 设置GIL2. 切换 ...
- hadoop学习一:hadoop安装(hadoop2.4.1,ubuntu14.04)
1.创建用户 adduser hduser 修改hduser用户权限: sudo vim /ect/sudoers ,在文件中增加 hduser ALL=(ALL:ALL) ALL .
- </2017><2018>
>>> Blog 随笔原始文档及源代码 -> github: https://github.com/StackLike/Python_Note >>> 统计信 ...
- 初识.NET Core
dotnet new console dotnet new web dotnet new webapi dotnet run dotnet build -r win-x64 dotnet publis ...
- 【块状树】bzoj3731 Gty的超级妹子树
带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...