P1168 中位数
P1168 中位数
树状数组+二分答案。
树状数组就是起一个高效查询比二分出来的数小的有几个。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.11.7
using namespace std;
int a[],b[],c[];
int n;
int t[];
int x,l,r,mid;
int now,Min;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=(x<<)+(x<<)+c-'',c=g();
x*=y;
} void modify(int k)
{
for(;k<=n;k+=(-k)&k)
t[k]++;
} int get(int k)
{
int cnt=;
for(;k>;k-=(-k)&k)
cnt+=t[k];
return cnt;
} void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
in(n);
For(i,,n)
in(a[i]),b[i]=a[i];
sort(b+,b+n+);
int len=unique(b+,b+n+)-b-;
For(i,,n)
{
x=a[i];
a[i]=lower_bound(b+,b+len+,a[i])-b;
c[a[i]]=x;
}
For(i,,n)
b[i]=;
o(c[a[]]),p('\n');
b[a[]]++;
modify(a[]);
for(int i=;i<=n;i+=)
{
b[a[i]]++;
b[a[i-]]++;
modify(a[i]);
modify(a[i-]);
l=,r=n;
while(l<r)
{
mid=(l+r)>>;
if(b[mid]==)
{
now=get(mid);
if(now*>i)
r=mid;
else
l=mid+;
}
else
if(b[mid]==)
{
now=get(mid-);
if(now*+==i)
{
o(c[mid]),p('\n');
break;
}
if(now*>i)
r=mid;
else
l=mid+;
}
else
{
now=get(mid);
Min=get(mid-);
if(now*>=i&&Min*<i)
{
o(c[mid]),p('\n');
break;
}
if(Min*>i)
r=mid;
else
l=mid+;
}
}
}
return ;
}
P1168 中位数的更多相关文章
- 洛谷——P1168 中位数
P1168 中位数 题目描述 给出一个长度为NN的非负整数序列$A_i$,对于所有1 ≤ k ≤ (N + 1),输出$A_1, A_3, …, A_{2k - 1}A1,A3,…,A2k−1 ...
- [luogu]P1168 中位数[堆]
[luogu]P1168 中位数 题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.即前1 ...
- 洛谷P1168 中位数——set/线段树
先上一波链接 https://www.luogu.com.cn/problem/P1168 这道题我们有两种写法 第一种呢是线段树,我们首先需要将原本的数据离散化,线段树维护的信息就是区间内有多少个数 ...
- 洛谷 P1168 中位数(优先队列)
题目链接 https://www.luogu.org/problemnew/show/P1168 解题思路 这个题就是求中位数,但是暴力会tle,所以我们用一种O(nlogn)的算法来实现. 这里用到 ...
- 洛谷P1168 中位数
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- LuoGu P1168 中位数
题目描述 给出一个长度为 $ N $ 的非负整数序列 $ A_i $ ,对于所有 $ 1 ≤ k ≤ (N + 1) / 2 $ ,输出 $ A_1, A_3, -, A_{2k - 1} $ 的中位 ...
- 【洛谷】【堆】P1168 中位数
[题目描述:] 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.即前1,3,5,……个数的中位数. ...
- P1168 中位数(对顶堆)
题意:维护一个序列,两种操作 1.插入一个数 2.输出中位数(若长度为偶数,输出中间两个较小的那个) 对顶堆 维护一个小根堆,一个大根堆,大根堆存1--mid,小根堆存mid+1---n 这样堆顶必有 ...
- [洛谷P1168]中位数(Splay)/(主席树)
Description 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], -, A[2k - 1]的中位数.即前1,3,5,--个数的 ...
随机推荐
- VBS将本地的Excel数据导入到SQL Server中
VBS将本地的Excel数据导入到SQL Server中 高文龙关注0人评论1170人阅读2017-05-14 12:54:44 VBS将本地的Excel数据导入到SQL Server中 最近有个测试 ...
- js new一个函数和直接调用函数的区别
用new和调用一个函数的区别:如果函数返回值是一个值类型(Number.String.Boolen)时,new函数将会返回这个函数的实例对象,而如果这个函数的返回值是一个引用类型(Object.Arr ...
- ?:,reverse,vector的基本小结
#include <cstdio> //此代码为网上所复制 #include <iostream> #include <string> #include <s ...
- vue中 裁剪,预览,上传图片 的插件
参考地址: https://github.com/dai-siki/vue-image-crop-upload
- django----常用功能
request.path_info 获取url地址
- cf1108E2 线段树类似扫描线
/* 有点像扫描线 思路:从左到右枚举每个点,枚举到点i时,把所有以i为起点的区间的影响删去 再加上以i-1为结尾的区间的影响 */ #include<bits/stdc++.h> usi ...
- 解决linux下svn update 产生Node remains in conflict的问题
提交一个文件 服务器上死活更新不了 是因为有冲突,解决办法:svn revert --depth=infinity /var/SvnProject/APITest 再次执行更新 进有改动的文件夹,更新 ...
- jmeter csv中获取带引号的数据详情
最近在工作中,对jmeter实践的点滴的记录这里分享,不一定正确,仅供参考和讨论,有想法的欢迎留言.谈论. 1技巧1:从csv中获取带引号的数据详情 背景:我们从csv中获取数据,在jmeter中使用 ...
- youtube-dl更新出错解决办法
youtube-dl更新命令: youtube-dl -U 更新报错:无法识别当前版本 ERROR: can't find the current version. Please try again ...
- 计蒜客 X的平方根(二分法)
设计函数int sqrt(int x),计算 xx 的平方根. 输入格式 输入一个 整数 xx,输出它的平方根.直到碰到文件结束符(EOF)为止. 输出格式 对于每组输入,输出一行一个整数,表示输入整 ...