Middle number
There is a sequence of integers, we have two operations now
1 add a: means add an integer a to the end of the sequence, forms a N+1 long sequence.
2 mid : Output the current sequence's middle numbera sequence's middle number is the middle position of the sequence when it's sorted by in creasing order.(if the sequence's length is even, then the middle position is the litter number's position of the two middle numbers)
example 1, sequence 1 2 13 14 15 16 and it's middle number is 13
example 2, sequence 1 3 5 7 10 11 17 and it's middle number is 7
example 3, 1 1 1 2 3 and it's middle number is 1
Input
The first line of the input gives the number of test cases T, for each test case the first line is the sequence's inital length N, the second line has N number represent the integer sequence. then third line is the operation number M then follows M lines, each line has the format either add a or mid (1<=N<=100000, 0<=M<=10000)
Output
each test case for each mid operation output the middle number
Sample Input
1
6
1 2 13 14 15 16
5
add 5
add 3
mid
add 20
mid
Sample Output
5
13
题目分析:数据结构题,使用一个大顶堆与一个小顶堆,时刻维护着大顶堆的元素小于小顶堆元素,并且大顶堆内元素的个与小顶内元素的个数相等或者比后者多一个,故每次查寻的结果都是大顶堆顶元素
n用STL实现堆操作
#include<iostream>
#include<set>
using namespace std;
multiset <int> small;
multiset <int, greater<int> > big;
int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
int n,num;
scanf("%d",&n);
small.clear();
big.clear();
int nt = n;
while(nt--){
scanf("%d",&num);
small.insert(num);
}
for(int i = 0; i < (n+1)/2; i++){
big.insert(*(small.begin()));
small.erase(small.begin());
}
int m;
scanf("%d",&m);//getchar();
char op[100];
while(m--){
scanf("%s",&op);
if(!strcmp(op, "add"))
{
scanf("%d",&num);
if(num > *big.begin()){
small.insert(num);
}
else
big.insert(num);
if(small.size() > big.size()){
big.insert(*small.begin());
small.erase(small.begin());
}
else if(big.size() > small.size() + 1){
small.insert(*big.begin());
big.erase(big.begin());
}
}
else
printf("%d\n",*(big.begin()));
}
}
return 0;
}
分享到:
相关推荐
通过实例介绍了 cast(multiset() as) 的使用方法,以处理嵌套表的操作
stl容器multiset的使用 包含6.0代码 以及详细的文档说明
MultiSet是一款界面简洁的自动程序安装工具。不需要编写程序,用这个程序可以是你从大量的程序安装过程中解放出来。并且可以在安装过程中实现注册信息的输入 Almeza MultiSet Pro 7.8.1绿色版 自动程序安装
安装说明:安装后先不要立即运行Almeza MultiSet,将内附的“activate_multiset.amltkey”文件复制到C:\Program Files\Almeza\MultiSet目录下面即是正式版。点击菜单View-->Language-->在General中选择...
Almeza MultiSet可通过软件安装管理器将常用程序集成到一起,它先录制软件的安装过程,下次安装同一软件时就会采用“回放”的方式... 小提示:如果需要将MultiSet中的所有程序安装到位,那么直接选择“安装所有”即可
python库,解压后可用。 资源全名:multiset_multicover-0.4-cp37-cp37m-win_amd64.whl
让你从软件安装中解放,让你从此一键安装千万软件 让你从软件安装中解放,让你从此一键安装千万软件
NULL 博文链接:https://zhang-zling.iteye.com/blog/327409
Almeza MultiSet Pro 是一个自动安装程序用一个简单和方便的接口。很多时候,它需要花费大量的时间,用户在安装操作系统后,安装必要的程序。并在同一时间,用户需要更换光盘的CD-ROM和DVD-ROM驱动器,输入注册数据...
STL_multiset 方法:multisetst; 定义了一个multiset变量st,st里面可以存放T类型数据,并且能自动排序。开始st为空 排序规则:表达式”a<b为true,则a排在b前面 可用的方法 目的 格式 添加元素 st.insert ...
python库,解压后可用。 资源全名:multiset_multicover-0.2-cp310-cp310-win32.whl
全自动软件安装,省去你安装软件的时间和烦恼
主要介绍了C++ STL入门教程第七篇,multimap一对多索引,multiset多元集合的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
解压即可使用 博文链接:https://heisetoufa.iteye.com/blog/368622
Java中的多集您只需要一个文件: : 因为我使用的是SpotBugs,所以有spotbugs-annotations-3.1.0.jar。 但是您也可以删除这些注释。 我的博客: :
Multiset ( 1 , 2 , 3 ) + Multiset ( 3 , 4 , 5 ) // == Multiset(1, 2, 3, 3, 4, 5) // Difference Multiset ( 1 , 2 , 3 ) - Multiset ( 2 , 3 ) // == Multiset(1) // Intersection Multiset ( 1 , 2 , 3 ) & ...
一共四个doc文件,list综合实例、multimap和map的实例、set和multiset的综合实例、vector综合实例
STL
用AVL-tree数据结构作为底层机制,以STL底层空间配置器和iterator_traits编程技法实作出一个独立的关联式容器(map, set, multimap, multiset),并对外提供接口实现和STL完全兼容的容器。