链接: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.

输入输出样例

输入样例#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[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)的更多相关文章

  1. LUOGU P2344 奶牛抗议 (树状数组优化dp)

    传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...

  2. Luogu P5103 「JOI 2016 Final」断层 树状数组or线段树+脑子

    太神仙了这题... 原来的地面上升,可以倒着操作(时光倒流),转化为地面沉降,最后的答案就是每个点的深度. 下面的1,2操作均定义为向下沉降(与原题意的变换相反): 首先这个题目只会操作前缀和后缀,并 ...

  3. hdu 3648 Median Filter (树状数组)

    Median Filter Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)

    https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...

  5. AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...

  6. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

  7. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

  8. ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)

    http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...

  9. AtCoder4351 Median of Medians 二分, 树状数组

    题目大意 定义一个从小到大的数列的中位数为第 $ \frac{n}{2}+1 $ 项.求一个序列的所有连续子序列的中位数的中位数. $ (n \leqslant 100000)$ 问题分析 由于\(n ...

随机推荐

  1. Python3发送webservice请求

    Python3使用suds-jurko库来发送webservice接口请求 导入请求webservice接口需要用到的包 pip install suds-jurko 第一步:导入所需要的包 from ...

  2. mysql日志文件路径

    SHOW VARIABLES LIKE 'general_log_file';日志文件路径SHOW VARIABLES LIKE 'log_error';错误日志文件路径SHOW VARIABLES ...

  3. 数据库学习之三--Select查询及运算符

    一.SELECT语句:用于从表中选取数据:语法如下: 1. 列查询: SELECT 列名称1,  列名称2 FROM 表名称: 2. 查询所有数据: SELECT * FROM 表名称: 3. 使用A ...

  4. 【説明する】KMP

    KMP是一个困扰我很久的算法,听老师或者是学姐讲了差不多有4次了,但是还是搞不太懂,今天终于,终于,终于搞懂了! ——2017-10-29 Vanora 首先推荐一下KMP详解——July 读罢之后内 ...

  5. Java面向对象3(K~O)

    K     正方形(SDUT 2444) import java.lang.reflect.Array; import java.util.*; public class Main { public ...

  6. Spring Cloud Eureka(五):Eureka Server 启动流程分析

    启用EurekaServer @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public st ...

  7. spring-boot 定时任务需要注意的地方

    spring-boot 跑定时任务非常容易 启动类上添加两个注解基本OK @EnableScheduling @EnableAsync 当然要记录的肯定不是这里的问题了 首先, fixedDelayf ...

  8. 第二次博客作业: 函数+进制转换器v1.0beta

    一:运行截图  二:介绍函数 1, int panduan1(int n,char a[],int count,int sign)//判断用户是否输入了除数字和a-f范围外的字符 { int i; ; ...

  9. linux查看服务安装目录redis

    如果用命令 which redis 或者 whereis redis 都找不到安装目录, 可使用以下办法: ps -aux | grep redis  或者ps -ef|grep redis 假如得到 ...

  10. zip flags 1 and 8 are not supported解决方案

    原因是因为使用了mac自带的软件打包成了zip,这种zip包unzip命令无法解压的. 所以解决方案就是使用zip命令进行压缩,zip -r 目标文件 源文件