洛谷P3031 [USACO11NOV](中位数,树状数组)
来源:互联网 发布:连线服务的网络提供商 编辑:程序博客网 时间:2024/06/05 15:16
题目地址:https://www.luogu.org/problemnew/show/3031
题目描述
Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure their heights; cow i has height H_i (1 <= H_i <= 1,000,000,000) nanometers--FJ believes in precise measurements! He wants to take a picture of some contiguous subsequence of the cows to submit to a bovine photography contest at the county fair.
The fair has a very strange rule about all submitted photos: a photograph is only valid to submit if it depicts a group of cows whose median height is at least a certain threshold X (1 <= X <= 1,000,000,000).
For purposes of this problem, we define the median of an array A[0...K] to be A[ceiling(K/2)] after A is sorted, where ceiling(K/2) gives K/2 rounded up to the nearest integer (or K/2 itself, it K/2 is an integer to begin with). For example the median of {7, 3, 2, 6} is 6, and the median of {5, 4, 8} is 5.
Please help FJ count the number of different contiguous subsequences of his cows that he could potentially submit to the photography contest.
给出一串数字,问中位数大于等于X的连续子串有几个。(这里如果有偶数个数,定义为偏大的那一个而非中间取平均)
输入格式:
Line 1: Two space-separated integers: N and X.
Lines 2..N+1: Line i+1 contains the single integer H_i.
输出格式:
Line 1: The number of subsequences of FJ's cows that have median at least X. Note this may not fit into a 32-bit integer.
输入输出样例
输入样例#1: 复制
4 6
10
5
6
2
输出样例#1: 复制
7
说明
FJ's four cows have heights 10, 5, 6, 2. We want to know how many contiguous subsequences have median at least 6.
There are 10 possible contiguous subsequences to consider. Of these, only 7 have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10, 5, 6}, {10, 5, 6, 2}.
【分析】:
dp的思想,树状数组优化。设数组dp[i]表示以第i元素结尾时,>=X的数量。
那么以第i元素结尾的符合条件的子序列要满足 大于X的数不少于小于X的数
则开头元素j需满足:2*(dp[i] - dp[j-1]) >= i - (j-1)
即 2*dp[i] - i >= 2*dp[j-1] - (j-1)
令 w = 2*dp[i] - i
只需结尾元素的 w 大于开头元素的 w ,树状数组维护即可
【代码】:
- 洛谷P3031 [USACO11NOV](中位数,树状数组)
- 洛谷P3031 中石油2557 Above the Median (数组模拟or树状数组求逆序数)
- 二分树状数组-洛谷P1168 中位数
- PAT 1057. Stack (30) 中位数;树状数组
- 考试题 树状数组查找以比x大的数作为中位数的个数+二分中位数
- 数组中位数
- 递归求两个数组的中位数(或下中位数)
- 洛谷P1908 逆序对(树状数组)
- 树状数组简介(洛谷P3368、P3374)
- 洛谷P1428 树状数组。。。
- 洛谷 模板 树状数组
- 洛谷P3374 树状数组
- 洛谷P3368 树状数组
- (转)树状数组
- 树状数组(转载)
- Stars(树状数组)
- 树状数组(interval)
- (转)树状数组
- android自定义view控件和自定义更改控件内容
- 3dmax模型导入unity3d
- MongoDB学习笔记(二)
- Android 7.0 调用系统拍照,相册,剪裁,修复拍照图片旋转问题
- 联想携手北京华联打造智慧商城 | Face++完成4.6亿美元C轮融资【软件网每日新闻播报│第11-1期】
- 洛谷P3031 [USACO11NOV](中位数,树状数组)
- LeetCode基础-查找
- 银行求本金和
- JDBC要点总结
- Napajs demo-单个生产者/消费者
- 美团云+AI打造最开放的人工智能平台,会带来哪些新变化?
- Python--day10 函数1
- Neo4j 爬坑笔记for3.2.6
- Python进阶三部曲之IO操作