链接: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. bootstrap-vue 中 model 基础用法

    Model 官方文档:  https://bootstrap-vue.js.org/docs/components/modal <b-modal v-model="labelModal ...

  2. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  3. 020_linux驱动之_输入子系统按键应用

    (一)分配一个输入子系统结构体 static struct input_dev *buttons_dev; /*分配一个input_dev结构体*/ (二)设置这个输入子系统需要的动作 /* 1. 分 ...

  4. The 10th Shandong Provincial Collegiate Programming Contest

    目录 Contest Info Solutions A. Calandar B. Flipping Game C. Wandering Robot D. Game on a Graph E. BaoB ...

  5. hive的两种使用方式

    hive的两种使用方式 1,hive shell的方式 启动命令: bin/hive 2.beeline客户端方式 首先在一个机器上启动hive thrift服务 bin/hiveserver2 在其 ...

  6. Docker安装redis3.2

    1.拉取redis3.2镜像 2.使用docker images查看拉去下来的镜像 3.运行容器,命令如下 docker run -p : -v $PWD/data:/data -d redis:3. ...

  7. 发现一个好的手机抓包工具Http Traffic

    ---恢复内容开始--- 晚上加班闲着没事,喜欢抓包,逛破解论坛,看到他们在聊Http Traffic手机抓包工具, 就下载了打算玩玩 Http Traffic: 是 HTTP 抓包调试工具 HTTP ...

  8. 了解Spring Boot的自动配置

    摘自:https://www.jianshu.com/p/ddb6e32e3faf Spring Boot的自动配置给开发者带来了很大的便利,当开发人员在pom文件中添加starter依赖后,mave ...

  9. vue不同序号的元素添加不同的样式

    vue不同序号的元素添加不同的样式 一.总结 一句话总结: 在vue中设计一个样式的数据数组来遍历即可 <script> new Vue({ el:'#review_exam_part', ...

  10. arcgis python获得别名

    import arcpy # Create a Describe object from the GDB table. # desc = arcpy.Describe(r"C:\Users\ ...