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 ...
随机推荐
- Django-DRF框架
一.RESTfull设计风格 1.什么是RESTfull? 1)REST:即Representational State Transfer的缩写.维基百科称其为"具象状态传输",国 ...
- PyQt(Python+Qt)学习随笔:QDockWidget停靠部件floating和features属性
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 1.floating属性 floating属性表示QDockWidge ...
- 第10.5节 使用__all__定义Python模块导入白名单
一. 引言 <第10.4节 Python模块的弱封装机制>介绍了Python模块的的弱封装机制,除了使用弱封装机制来从一定程度上防止导入特定成员外,Python模块中还提供可另外一种类似白 ...
- PyQt(Python+Qt)学习随笔:QTreeView树形视图的expandsOnDoubleClick属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的expandsOnDoubleClick属性用于控制鼠标双击是否展开或折 ...
- 项目测试环境自动化部署[jenkins前后端配置、Nginx配置]
持续部署:关注点在于项目功能部署到服务器后可以正常运行,为下一步测试环节或最终用户正式使用做准备.(问题点:一个环节有问题,其他环节跟着有问题) 持续集成:关注点是在于尽早发现项目整体运行问题,尽早解 ...
- Docker 安装-在centos7下安装Docker(二)
参考docker安装的方式: http://www.runoob.com/docker/centos-docker-install.html Docker中文官网安装步骤:https://docs.d ...
- 箭头函数this指向问题
this指向本质 箭头函数this指向的固定化,并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this,导致内部的this就是外层代码块的this.正是因为它没有this ...
- MySQL 锁(完整版)
目录 锁总览 锁的作用 加锁流程 锁对数据库的影响 锁等待 死锁 锁类型 锁范围 锁方式 全局锁 全局读锁 全局QC锁 QC锁存在的问题: 备份锁 backup lock MDL锁 MDL锁类型 MD ...
- Pytest 学习(二十七)- Jenkins+Allure+Pytest的持续集成
一.配置 allure 环境变量 1.下载 allure是一个命令行工具,可以去 github 下载最新版:https://github.com/allure-framework/allure2/re ...
- 设置定时任务用rman删除归档日志脚本
之前使用数据库数据迁移过程中出现产生大量归档日志的情况(由于迁移的目标库是DG,必须开启归档). 为避免出现归档空间爆掉的情况,设置定时任务删除系统当前时间30分钟前的归档日志,脚本如下: cat d ...