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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  5. 【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 ...

  6. 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 ...

  7. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  8. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  9. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

  10. 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 ...

随机推荐

  1. ubantu+nginx+uwsgi+django部署

    1.更新ubantu的apt apt-get update 必要时候更新系统:      apt-get upgrade 2.远程连接服务器 ssh 用户名@ip 上传代码 :        scp ...

  2. url的组成结构信息

    http://www.aspxfans.com:8080/news/index.asp?boardID=5&ID=24618&page=1#name 从上面的URL可以看出,一个完整的 ...

  3. SQL Server 批量插入数据方案 SqlBulkCopy 的简单封装,让批量插入更方便

    一.Sql Server插入方案介绍 关于 SqlServer 批量插入的方式,有三种比较常用的插入方式,Insert.BatchInsert.SqlBulkCopy,下面我们对比以下三种方案的速度 ...

  4. IDM 6.37.8 绿色特别版 (4月4日更新)

    Internet Download Manager,全球最流行的下载工具.Internet Download Manager (简称IDM) Windows 平台功能强大的多线程下载工具,国外非常受欢 ...

  5. KVM初体验之virt-manager unable to connect to libvirt的处理办法

    解决方法 需要用root身份运行virt-manager

  6. SpringBoot使用Mybatis-plus及分页功能的细节部分

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  7. ModelViewSet+ModelSerializer使用

    1.DRF初始化 DRF框架的8个核心功能 1.认证(用户登录校验用户名密码或者token是否合法) 2.权限(根据不同的用户角色,可以操作不同的表) 3.限流(限制接口访问速度) 4.序列化(返回j ...

  8. linux下postgresql安装

    1.下载地址 https://www.enterprisedb.com/download-postgresql-binaries 2.创建用户并修改密码 [root@node01 ~]# userad ...

  9. html 03-初识HTML

    03-初识HTML #本文主要内容 头标签 排版标签:<p>. <div>. <span>.<br> . <hr> . <center ...

  10. css 06-CSS盒模型详解

    06-CSS盒模型详解 #盒子模型 #前言 盒子模型,英文即box model.无论是div.span.还是a都是盒子. 但是,图片.表单元素一律看作是文本,它们并不是盒子.这个很好理解,比如说,一张 ...