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 ...
随机推荐
- SIGAI深度学习第九集 卷积神经网络3
讲授卷积神经网络面临的挑战包括梯度消失.退化问题,和改进方法包括卷积层.池化层的改进.激活函数.损失函数.网络结构的改 进.残差网络.全卷机网络.多尺度融合.批量归一化等 大纲: 面临的挑战梯度消失问 ...
- P3355 骑士共存问题【洛谷】(二分图最大独立集变形题) //链接矩阵存图
展开 题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可 ...
- VS tools
官方下载,有免费也有试用的 http://visualstudiogallery.msdn.microsoft.com/ VS2012简单的使用感受+插件推荐 http://blog.sina.com ...
- python pass del eval
pass python中空代码块是非法的,解决的方法就是在语句块中加上一个pass语句 eval >>> eval("print('hellowrold')")h ...
- adb命令积累
1. 模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: (原文:http://blog.csdn.net/huiguixian/article/details/1192538 ...
- pandas入门之DataFrame
创建DataFrame - DataFrame是一个[表格型]的数据结构.DataFrame由按一定顺序排列的多列数据组成.设计初衷是将Series的使用场景从一维拓展到多维.DataFrame既有行 ...
- slax linux的定制
由于数据结构教学的需要,需要用到linux,要求就是小,启动快,可定制性强,恰好slax正好满足要求,以下就是定制slax linux的过程记录: 什么是Slax Slax是一个基于Linux的Liv ...
- mysql之group_concat函数
mysql之group_concat函数 在介绍GROUP_CONCAT之前,我们先来看看concat()函数和concat_ws()函数. 先准备一个测试数据库: mysql> select ...
- mvc partialView断点调试问题
mvc中的partialview 在前端f12调试时,默认看不到代码的. 在Js中加上debugger; 调试时会走到断点,多出个VM打头的局部视图页面.
- ArcGIS Python 唯一值专题
import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...