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 ...
随机推荐
- 第15.28节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QTableWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 表格部件为应用程序提供标准的表格显示工具,在表格内可以管理基于行和列的数据项,表格中的最大 ...
- Django官方为什么没有标准项目结构
Django官方并没有提供标准的项目结构,于是网上众说纷纭,百花齐放,一千个读者有一千个哈姆雷特.那我们该怎么设计项目结构呢?在回答这个问题之前,先了解一下Django原生的目录和文件都是干嘛的. p ...
- 半夜删你代码队 Day5冲刺
一.每日站立式会议 1.站立式会议 成员 昨日完成工作 今日计划工作 遇到的困难 陈惠霖 跟进作业 完成注册界面 相关界面设计内容知识不充足 侯晓龙 开始双人合作 开始与数据库结合 无 周楚池 初步完 ...
- jquery和zepto有何区别?
1.针对移动端程序,Zepto有一些基本的触摸事件可以用来做触摸屏交互(tap事件.swipe事件),Zepto是不支持IE浏览器的. 2.DOM操作的区别:添加id时jQuery不会生效而Zepto ...
- 题解-洛谷P6788 「EZEC-3」四月樱花
题面 洛谷P6788 「EZEC-3」四月樱花 给定 \(n,p\),求: \[ans=\left(\prod_{x=1}^n\prod_{y|x}\frac{y^{d(y)}}{\prod_{z|y ...
- Java集合源码分析(一)——集合框架
集合框架 集合框架如图所示 Java集合是Java提供的工具包,主要包括常用的数据结构,包括:集合.链表.队列.栈.数组.映射等. 集合的工具包位置是java.util.* 集合主要可以分为五类: L ...
- mongodb 学习之——mongod --help 中文解释
中文解释如下: -------------------------------------------------------------------------------- --quiet # 安 ...
- Javascript的解码/编码api
1.fromCharCode() 接受一个指定的 Unicode 值,然后返回一个字符串. 语法:String.fromCharCode(numX,numX,...,numX) 详细了解:http:/ ...
- 使用plesk遇到的问题
按照plesk使用指南中,"快速建站"的部分,配置一番后,还是访问不了网站. 后来解决了,原因如下: 主域名没有解析,只解析了,带www的子域名 80端口没开
- GitLab的基本了解和使用
使用前提 GitLab账号 安装好git VSCODE && 配置GitPath 在vscode里配置GitPath file-preferences-setting 在search ...