luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)
链接:https://www.luogu.org/problemnew/show/P3031
题面:
题目描述
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.
输入输出样例
说明
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[i]是从1开始以i结尾的串中大于等于k的有多少个,如果以i结尾以j开始的序列要让中位数大于等于k,那么大于等于k的数的个数就要 > 小于k的数的个数,我们可以得到关系式:
2*(dp[i]-dp[j-1]) >= i-j+1
化简为: 2*dp[i] - i >= 2*dp[j-1] - (j-1) 设 x = 2*dp[i]-i; 注意 -n <= x <= n, 我们把它加上n+1,这样就变成了 1<= x <= 2*n+1, 然后扔到树状数组上就好了,那么当i的x大于j的x,那么区间[j+1,i]这段区间是符合要求的,我们需要在n+1这个点+1,(判断1-i区间是否符合要求)用树状数组统计下从1到当前点有多少个比当前数小的数字,那么就可以得到以i结尾有多少符合题意的序列。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 2e5+;
ll c[M],dp[M],n;
void add(ll x,ll val){
while(x <= *n+){
c[x] += val;
x += (x&-x);
}
} ll getsum(ll x){
ll ans = ;
while(x){
ans += c[x];
x -= (x&-x);
}
return ans;
}
int main()
{
ll k,x;
while(cin>>n>>k){
memset(c,,sizeof(c));
dp[] = ;
ll ans = ; add(n+,);
for(ll i = ;i <= n;i ++){
cin>>x;
if(x >= k) dp[i] = dp[i-]+;
else dp[i] = dp[i-];
ans += getsum(*dp[i]-i+n+);
add(*dp[i]-i+n+,);
}
cout<<ans<<endl;
}
return ;
}
luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)的更多相关文章
- LUOGU P2344 奶牛抗议 (树状数组优化dp)
传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...
- Luogu P5103 「JOI 2016 Final」断层 树状数组or线段树+脑子
太神仙了这题... 原来的地面上升,可以倒着操作(时光倒流),转化为地面沉降,最后的答案就是每个点的深度. 下面的1,2操作均定义为向下沉降(与原题意的变换相反): 首先这个题目只会操作前缀和后缀,并 ...
- hdu 3648 Median Filter (树状数组)
Median Filter Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)
https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...
- AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- 【树状数组逆序对】USACO.2011JAN-Above the median
[题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...
- ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)
http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...
- AtCoder4351 Median of Medians 二分, 树状数组
题目大意 定义一个从小到大的数列的中位数为第 $ \frac{n}{2}+1 $ 项.求一个序列的所有连续子序列的中位数的中位数. $ (n \leqslant 100000)$ 问题分析 由于\(n ...
随机推荐
- 关系型数据库与NoSQL的对比
SQL(结构化的查询语言)数据库是过去四十年间存储数据的主要方式.20世纪90年代末随着Web应用和MySQL.PostgreSQL和SQLite等开源数据库的兴起,用户爆炸式的增长. NoSQL数据 ...
- luoguP4778 Counting swaps
题目链接 题解 首先,对于每个\(i\)向\(a[i]\)连边. 这样会连出许多独立的环. 可以证明,交换操作不会跨越环. 每个环内的点到最终状态最少交换步数是 \(环的大小-1\) 那么设\(f[i ...
- 无缓存I/O操作和标准I/O文件操作区别
本文转载于:http://www.360doc.com/content/11/0521/11/5455634_118306098.shtml 首先,先稍微了解系统调用的概念: 系统调用,英 ...
- Luogu5327【ZJOI2019】语言【树上差分,线段树合并】
题目大意 给定一棵$n$个节点的树,维护$n$个集合,一开始第$i$个集合只有节点$i$.有$m$个操作,每次操作输入一个$(u,v)$,表示将$(u,v)$这条链上所有点所属的集合合并.求有多少个无 ...
- JVM——垃圾回收
目录: 如何判断垃圾是否回收? 引用计数法 可达性分析算法 四种引用 引用队列 垃圾回收算法 标记清除算法 复制算法 标记整理算法 分代垃圾回收 新生代 老年代 Minor GC 和 Full GC的 ...
- table表格整体居中
代码: <div style="text-align:center"> <table border="1" cellpadding=" ...
- 两大主流开源分布式存储的对比:GlusterFS vs. Ceph
两大主流开源分布式存储的对比:GlusterFS vs. Ceph 存储世界最近发生了很大变化.十年前,光纤通道SAN管理器是企业存储的绝对标准,但现在的存储必须足够敏捷,才能适应在新的基础架构即服务 ...
- Java实例化对象过程中的内存分配
Java实例化对象过程中的内存分配: https://blog.csdn.net/qq_36934826/article/details/82685791 问题引入这里先定义一个很不标准的“书”类,这 ...
- ICEM-五通孔管
原视频下载地址:https://yunpan.cn/cqaQ2t5DrRcKa 访问密码 d111
- export default {} 和new Vue()区别?
export default 的用法:相当于提供一个接口给外界,让其他文件通过 import 来引入使用. 而对于 new Vue({})部分, 只是创建一个Vue的实例 就是相当于创建一个根组件 h ...