[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 3 Hint For 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 ...
随机推荐
- opencv-写入AVI视频文件
#include <cv.h> #include <highgui.h> int main(int argc, char **argv) { CvCapture* captur ...
- selenium 下拉框处理
web应用中有很多时候我们会遇见<select></select>标签的下列列表框,一般是无法直接去操作下列列表中的选择的.selenium webdriver 提供了专门操作 ...
- docker从零开始(三)服务初体验docker compose
决条件 安装Docker 1.13或更高版本. 获取Docker Compose.在适用于Mac的Docker和适用于Windows的Docker上,它已预先安装,因此您可以随意使用.在Linux系统 ...
- LUA脚本中O(2)级素数查询
--================================================================================================== ...
- Gitlab,这也就O了???
最简单配置也是一句话搞了... rpm -i gitlab-ce--ce..el7.x86_64.rpm vim /etc/gitlab/gitlab.rb gitlab-ctl reconfigur ...
- python实用技巧 : Filtering os.walk(转)
''' Created on Mar 7, 2010 @author: Diego 需求: 得到某个目录下, 符合过滤条件的文件夹/文件.实现: 将os.walk再次包装. TODO: 不知道本程序的 ...
- ZCMU新人训练赛-B
Tom's Meadow Tom has a meadow in his garden. He divides it into N * M squares. Initially all the sq ...
- Ubuntu 安装 tensorflow-gpu + keras
转载请注明: 仰望大牛的小清新:http://www.cnblogs.com/luruiyuan/ 本文原网址:http://www.cnblogs.com/luruiyuan/p/6660142.h ...
- FFT&NTT&多项式相关
打了FFT 感觉以后多项式不虚了 ~滑稽~ PS 关于详见没写完.... code #include<cmath> #include<cstdio> #include<c ...
- [CF915F]Imbalance Value of a Tree
[CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...