Above the Median
http://www.forioi.com/p/3212
农夫约翰把他的N(1<=N<=1e5)奶牛排在一排来衡量他们的高度,牛i有:高度H_I(1<=H_I<=1e9)纳米–因为FJ认为他需要精确测量!他想选择一些连续的奶牛拍一张照片发给牛摄影大赛。大赛有一个很奇怪的规则,对所有提交的照片:照片有效当且仅当,它描绘了一群中位身高至少大于一定的阈值X(1<=x<=1e9)的奶牛。中位身高定义为:有n头奶牛按从小到大顺序排好,第[(1+n)/2](取上限)头奶牛的身高。例如{7,3,2,6}的中位数是6,和{5,4,8}的中位数是5。FJ想知道他有多少种选择。
输入
*第1行:两个用空格隔开的整数:N和X
*第2 .. N 1:第i行1包含单个整数H_I。
输出
*第1行:选择的个数,注意,该数可能超出32位整数的存储范围。
样例
输入
复制
4 6
10
5
6
2
输出
复制
7
提示
有10个可能选择。其中,只有7 个的中位数大于6。它们是{10},{6},{10,5},{5,6},{6,2},{10, 5,6},{1
0,5,6,2}
首先我们分析一下这一个题
假如有n个数,那么只需要有n/2个数比X大就可以满足条件
我们把设置一个num[i]来表示前i个数大于等于x的有几个,
只要这个数大于等于x,num[i]=num[i-1]+1,else num[i]=num[i-1]-1
我们可以发现,只要num[i]>=0那么就可以满足条件,ans++
比如前五个数是:3 2 4 6 1 x是3
那么前1个数的num[1]=num[0]+1(3>=3)
那么前2个数的num[2]=num[1]-1(2<3)
那么前3个数的num[3]=num[2]+1(4>=3)
那么前4个数的num[4]=num[3]+1(6>=3)
那么前5个数的num[5]=num[4]-1(1<3)
那如果是求第2个到第5个是否满足条件呢
那我么就可以把前5个数的num减去前1个数的num(2也在这个区间,所以不需要减去)
也就是num[5]-num[2-1] =1-1=0
所以求第i到j个的和就可以直接得出=num[j]-num[i-1]
求num
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]>=x) num[i]=num[i-1]+1;
else num[i]=num[i-1]-1;
}
所以我们只需要做一遍查找,看枚举num[j]-num[i]是否大于0
for(int j=1;j<=n;j++)
for(int i=1;i<=j;i++)
if(num[j]-num[i]>=0)
ans++;
但这样太慢了。。。
我们来想一下优化:
其实查找就是找前i个有多少个小于num[i],我们可以来用树状数组来优化
不会写树状数组请看https://www.cnblogs.com/cwjr/p/13230091.html
#include<bits/stdc++.h>
using namespace std;
const long long N=1e5*3;
long long c[N],ans,n,m,num;
long long lowbit(long long x){
return x&(-x);
}
void insert(long long x,long long vol){
while(x<=N){
c[x]+=vol;
x+=lowbit(x);
}
}
long long ask(long long x){
long long sum=0;
while(x){
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int main(){
cin>>n>>m;
//树状数组下标整体加上n+1,防止出现负数
insert(n+1,1); //把0加入树状数组,请c思考why
for(long long i=1;i<=n;i++)
{
long long x;
cin>>x;
if(x>=m)
num++;
else num--;
ans+=ask(num+n+1);
insert(num+n+1,1);
}
cout<<ans;
}
Above the Median的更多相关文章
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 我在苦苦坚持的时候,WebStorm已经悄悄的“真香”起来
前言 最近接了一个活儿,是用WebStorm开发一个基于VUE的网站,但是我真的是几乎没接触过VUE相关的项目实践,更别说用WebStorm在实际中的应用,之前只是听朋友说多好用,但是,因为现有工具不 ...
- Python的富比较方法__le__、__ge__之间的关联关系分析
Python的富比较方法包括__le__.__ge__分别表示:小于等于.大于等于,对应的操作运算符为:"<=".">=".那么是否象普通数字运算一 ...
- 第14.5节 利用浏览器获取的http信息构造Python网页访问的http请求头
一. 引言 在<第14.3节 使用google浏览器获取网站访问的http信息>和<第14.4节 使用IE浏览器获取网站访问的http信息>中介绍了使用Google浏览器和IE ...
- PyCharm中怎么将非当前工程文件的目录的文件加到当前工程中
在PyCharm已经建立工程文件的情况下,如果要将一个其他目录的文件导入到已有的工程中,唯一的方法如下: 通过File->Settings->Project->Project Str ...
- 打造云原生大型分布式监控系统(四): Kvass+Thanos 监控超大规模容器集群
概述 继上一篇 Thanos 部署与实践 发布半年多之后,随着技术的发展,本系列又迎来了一次更新.本文将介绍如何结合 Kvass 与 Thanos,来更好的实现大规模容器集群场景下的监控. 有 Tha ...
- python自带缓存lru_cache用法及扩展(详细)
本篇博客将结合python官方文档和源码详细讲述lru_cache缓存方法是怎么实现, 它与redis缓存的区别是什么, 在使用时碰上functiontools.wrap装饰器时会发生怎样的变化, ...
- 剑指offer二刷——数组专题——数字在升序数组中出现的次数
题目描述 统计一个数字在升序数组中出现的次数. 我的想法 完整的解法我只想到了遍历数组然后依次统计,但这是不聪明的解法,而且没有利用上"升序数组"的这个条件. 题目标签有提醒可以用 ...
- Java 线程安全问题的本质
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录: 线程安全问题的本质 理解CPU JVM虚拟机类比于操作系统 重排序 汇总 一些解释 ...
- 开源性能监控分析工具glowroot
最近在做java性能瓶颈定位分析工具的研究,发现glowroot工具是一款相当不错的APM工具(Wonderful tool),架构简洁,部署简单,上手容易. 经过亲身搭建体验,总结了产品的架构,工具 ...
- 初入Nginx--配置篇
Nginx的主配置文件为/path/to/nginx/nginx.conf.Nginx.conf的配置文件结构主要由以下几个部分组成: ..... events{ .... } http{ .... ...