洛谷——P3871 [TJOI2010]中位数
P3871 [TJOI2010]中位数
一眼秒掉,这不是splay水题吗,套模板
#include<bits/stdc++.h> #define IL inline
#define N 150005
using namespace std; int ch[N][],siz[N],val[N],cnt[N],f[N],root,ncnt; IL void pushup(int k) {
siz[k]=siz[ch[k][]]+siz[ch[k][]]+cnt[k];
} IL int chk(int x) {
return ch[f[x]][]==x;
} IL void rotate(int x) {
int y=f[x],z=f[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w,f[w]=y;
ch[z][chk(y)]=x,f[x]=z;
ch[x][k^]=y,f[y]=x;
pushup(y),pushup(x);
} IL void splay(int x,int goal=) {
while(f[x]!=goal) {
int y=f[x],z=f[y];
if(z!=goal) {
if(chk(x)==chk(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) root=x;
} IL void insert(int x) {
int cur=root,p=;
while(cur&&x!=val[cur]) {
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur) {
cnt[cur]++;
} else {
cur=++ncnt;
if(p) ch[p][x>val[p]]=ncnt;
f[cur]=p,siz[cur]=cnt[cur]=;
ch[cur][]=ch[cur][]=;
val[cur]=x;
}
splay(cur);
} IL int kth(int k)
{
int cur=root;
while(){
if(ch[cur][]&&k<=siz[ch[cur][]]){
cur=ch[cur][];
}else if(k>siz[ch[cur][]]+cnt[cur]){
k-=siz[ch[cur][]]+cnt[cur];
cur=ch[cur][];
}else return cur;
}
} int n,m; int main() {
scanf("%d",&n);
for(int x,i=; i<=n; i++) {
scanf("%d",&x);
insert(x);
}
scanf("%d",&m);
char opt[];
for(int x,i=; i<=m; i++) {
scanf("%s",opt);
if(opt[]=='a') scanf("%d",&x),insert(x),++n;
else printf("%d\n",val[kth(n/+(n%))]);
} return ;
}
开两个堆维护,大根堆维护小的中的最大值,小根堆维护打的中的最小值
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue> #define IL inline
#define N 150005
using namespace std; int n,m; priority_queue<int,vector<int> >q_1;//大根堆维护小值
priority_queue<int,vector<int>,greater<int> >q_2;//小根堆维护大值 void cz() {
while() {
int a=q_1.size(),b=q_2.size();
if(abs(a-b)<=) break;
if(q_1.size()>q_2.size()) q_2.push(q_1.top()),q_1.pop();
else q_1.push(q_2.top()),q_2.pop();
}
} int main() {
scanf("%d",&n);
for(int x,i=; i<=n; i++) {
scanf("%d",&x);
if(i==) q_1.push(x);
else {
if(x>q_1.top()) q_2.push(x);
else q_1.push(x);
cz();
}
}
scanf("%d",&m);
char opt[];
for(int x,i=; i<=m; i++) {
scanf("%s",opt);
if(opt[]=='a'){
scanf("%d",&x);
if(x>q_1.top()) q_2.push(x);
else q_1.push(x);
cz();
}
else {
if(q_1.size()!=q_2.size()) q_1.size()>q_2.size()?printf("%d\n",q_1.top()):printf("%d\n",q_2.top());
else printf("%d\n",min(q_2.top(),q_1.top()));
}
} return ;
}
洛谷——P3871 [TJOI2010]中位数的更多相关文章
- 洛谷 P3871 [TJOI2010]中位数 解题报告
P3871 [TJOI2010]中位数 题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前 ...
- 洛谷P3871 [TJOI2010]中位数(splay)
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- 洛谷 3871 [TJOI2010]中位数
[题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...
- 洛谷3871 [TJOI2010]中位数 维护队列的中位数
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- 洛谷 P3871 中位数
->题目链接 题解: 暴力 经鉴定,此题数据水到没朋友. #include<algorithm> #include<iostream> #include<cstdi ...
- 洛谷 P1627 [CQOI2009]中位数 解题报告
P1627 [CQOI2009]中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式 ...
- 洛谷——P1627 [CQOI2009]中位数
P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...
- [LUOGU] P3871 [TJOI2010]中位数
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- 洛谷 P3879 [TJOI2010]阅读理解
P3879 [TJOI2010]阅读理解 题目描述 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. 输入输出 ...
随机推荐
- the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
the largest value you actually can transmit between the client and server is determined by the amoun ...
- mouse_event function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx
- CANopen——总线基本知识
1. 总线标准 2. 获取索引和子索引 2fh,2bh,23h,40h等,是不是对应cs的不同值: 主站1280h的对象字典?1280h-sub2,得到client的COB-ID值: 根据收到的m-& ...
- 利用JProfile 7分析内存OOM
今天发现一个OOM问题,想来可能是有部分内存没有正确释放导致的 于是想到了之前使用的JProfile来分析一下 由于大概知道哪个类没有释放,可以在Memory Views里看一下对应的类有多少实例来确 ...
- nrm -- NPM registry 管理工具
https://cnodejs.org/topic/5326e78c434e04172c006826 cnpm:http://npm.taobao.org/
- bzoj 1231: [Usaco2008 Nov]mixup2 混乱的奶牛【状压dp】
设f[i][j]为奶牛选取状态为i,最后一头选的为j,转移直接f[k][(1<<(k-1)|i]+=f[j][i] #include<iostream> #include< ...
- O - Combinations (组合数学)
Description Computing the exact number of ways that N things can be taken M at a time can be a great ...
- Storm概念学习系列之storm的优化
不多说,直接上干货!
- [转]URL最大长度问题
本文转自:http://www.cnblogs.com/henryhappier/archive/2010/10/09/1846554.html 今天在测试Email Ticket的时候发现在进行Ma ...
- iOS 项目中的常见文件
iOS的笔记-项目中的常见文件 新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...