BZOJ 1303: [CQOI2009]中位数图
来源:互联网 发布:linux lvm扩容 编辑:程序博客网 时间:2024/06/05 23:47
BZOJ 1303: [CQOI2009]中位数图
题目
Time Limit: 1 Sec Memory Limit: 162 MB
Description
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。
Input
第一行为两个正整数n和b ,第二行为1~n 的排列。
Output
输出一个整数,即中位数为b的连续子序列个数。
Sample Input
7 4
5 7 2 4 3 1 6
Sample Output
4
HINT
第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000
题解
首先,我们很容易想到一个暴力,先写一个相比较于数字b的前缀和,再枚举数字b(所在位置为p)左边的i个数字和右边的j个数字,使得sum[p]-sum[i-1]=sum[j]-sum[p-1]
很显然,用上面的这个方法,会超时
然后我就想到了hash,用hash记录下从位置p到位置1相比较于数字b的前缀和的值出现的次数,再枚举从位置p到位置n相比较于数字b的和t,ans:=ans+hash[t];
时间复杂度O(N)
代码(Pascal)
var n,m,ans:longint; x:array[0..100005]of longint; hash:array[-100005..100005]of longint; i,t,p:longint; begin //assign(input,'1303.in');reset(input); //assign(output,'1303.out');rewrite(output); readln(n,m);t:=0; for i:=1 to n do begin read(x[i]); if x[i]=m then p:=i; end; inc(hash[0]); for i:=p-1 downto 1 do begin if x[i]>m then inc(t) else dec(t); inc(hash[t]); end; t:=0; for i:=p to n do begin if x[i]>m then dec(t) else if x[i]<m then inc(t); ans:=ans+hash[t]; end; write(ans); //close(input);close(output); end.
1 0
- 【BZOJ 1303】 [CQOI2009]中位数图
- bzoj 1303 [CQOI2009]中位数图
- BZOJ 1303 [CQOI2009]中位数图
- 【BZOJ 1303】 【CQOI2009】中位数图
- BZOJ 1303: [CQOI2009]中位数图
- bzoj 1303: [CQOI2009]中位数图
- BZOJ 1303 [CQOI2009]中位数图
- BZOJ 1303 - [CQOI2009]中位数图 (技巧)
- 【BZOJ】1303 [CQOI2009]中位数图 建模
- BZOJ 1303: [CQOI2009]中位数图 计数,思维
- bzoj 1303: [CQOI2009]中位数图 (乱搞)
- 1303: [CQOI2009]中位数图
- 1303: [CQOI2009]中位数图
- BZOJ 1303 [CQOI2009] 中位数图 题解与分析
- BZOJ_P1303 [CQOI2009]中位数图(中位数)
- [BZOJ1303][CQOI2009]中位数图
- [BZOJ1303][CQOI2009]中位数图
- BZOJ1303[CQOI2009]中位数图
- LeetCode 102. Binary Tree Level Order Traversal
- [项目实训]Django学习
- AngularJS Basic
- MySQL10-视图
- Android 7.0 ActivityManagerService 广播(Broadcast)相关流程分析
- BZOJ 1303: [CQOI2009]中位数图
- Eclispe4.5 servlet web开发_非Eclipse下构建OSGI运行环境
- Windows更新后,chrome无法联网
- undefined和null的区别
- F
- 树
- 【数据结构】中的堆(HeapSort)排序
- 虚拟机搭建CDH-第六讲-安装mysql第一种方法(比较简单)
- XTUOJ1264:Partial Sum(前缀和)