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 ...
随机推荐
- MapReduce怎么优雅地实现全局排序
思考 想到全局排序,是否第一想到的是,从map端收集数据,shuffle到reduce来,设置一个reduce,再对reduce中的数据排序,显然这样和单机器并没有什么区别,要知道mapreduce框 ...
- C++ 虚基类的定义、功能、规定
原文声明:http://blog.sina.com.cn/s/blog_93b45b0f01011pkz.html 虚继承和虚基类的定义是非常的简单的,同时也是非常容易判断一个继承是否是虚继承的,虽然 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项列图标的访问方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项中可以有多列数据,每列数据都可以设 ...
- 第十九章、Model/View开发:QTableView的功能及属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在Designer的部件栏Item Views中提供了PyQt和Qt已经实现好的table ...
- Python中数字按位取反的方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Python中有个按位取反运算符:,但这个运算符并不是真正的按位取反,而是效果相当于原值乘以负一再减 ...
- 建立SQL Server用户登录
第一步:右击服务器名,点击"属性",选择"安全性",选中"Sql server和Windows身份验证模式". 第二步:鼠标右键点击安全性中 ...
- 实验吧 Forms
0x1考察知识 html中value的作用 按钮中用的value 指的是 按钮上要显示的文本 比如'确定,删除 等等字样' 复选框用的value 指的是 这个复选框的值 单选框用的value 和复选框 ...
- Java进阶学习之集合与泛型(1)
目录 1.集合 1.1.集合是什么 1.2.集合框架结构 1.2.1.Collection 1.2.2.Map 1.3.集合接口实现类 1.3.1.LinkedList 1.3.2.ArrayList ...
- Hangfire&Autofac与ASP.NET CORE注入失败
Hangfire.Autofac与ASP.NET CORE注入失败 项目里面使用了Hangfire,因为之前没用过吧,遇到了个问题,就是使用了ico容器后,再用Hangfire总是注入不上对象,总是后 ...
- SpringCloud-服务间通信方式
接下来在整个微服务架构中,我们比较关心的就是服务间的服务改如何调用,有哪些调用方式? 总结:在springcloud中服务间调用方式主要是使用 http restful方式进行服务间调用 1. 基于R ...