HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)

时间:2021-10-30 20:36:33

The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

【Problem Description】
Xiao  Ming  and  Xiao  Bao  are  playing  a  simple  Numbers  game.  In  a  round  Xiao  Ming  can choose  to  write  down  a  number,  or  ask  Xiao  Bao  what  the  kth  great  number  is.  Because  the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 
【Input】
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
 
【Output】
The output consists of one integer representing the largest number of islands that all lie on one line.
 
【Sample Input】
I
I
I
Q
I
Q
I
Q

【Sample Output】


【Hint】

Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).

【题意】

给出一系列操作:

1.记录一个数;2.求第k小的数

【分析】

解法一、

题目每次询问的只是其中的一个数,这种情况下用一个堆来维护所有数的集合即可。

而且本题的k是一个固定值,因此只需要一个小根堆即可;若k不是一个固定值,则需要一个小根堆配合大根堆共同完成。

堆可以用STL中的优先队列来代替。

创建一个小根堆并向其加入数据,若堆中的数量大于k则弹出堆顶元素,始终保持整个堆中只有k个元素。遇到询问时,读取堆顶元素。

 /* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU4006
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; struct cmp
{
bool operator()(int x,int y)
{
return x>y;
}
}; int main()
{
//freopen("1.txt","r",stdin);
//freopen("std.txt","w",stdout); int n,k; while(scanf("%d%d",&n,&k)==)
{
priority_queue<int,vector<int>,cmp>q;
char c;
for (int i=;i<=n;i++)
{
c=getchar();
while(c!='I'&&c!='Q')
{
c=getchar();
}
if (c=='I')
{
int now;
scanf("%d",&now);
q.push(now);
if (q.size()>k) q.pop();
} else
{
printf("%d\n",q.top());
}
}
} return ;
}

解法二、

本题可作为平衡树模板题,虽然因为有点大材小用内存占用比较大,而且时间上并没有太大优势。

动态维护一棵平衡树,求k大值。

以下采用Size Balanced Tree完成:

 /* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU4006_SBT
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define MAXN 1000010 typedef struct sbtnod
{
int key,left,right,size;
} sbtnode;
int sbttail,sbt; sbtnode tree[MAXN]; void rrotate(int& t)
{
int k=tree[t].left;
if (!k) return ;
tree[t].left=tree[k].right;
tree[k].right=t;
tree[k].size=tree[t].size;
tree[t].size=tree[tree[t].left].size+tree[tree[t].right].size+;
t=k;
} void lrotate(int& t)
{
int k=tree[t].right;
if (!k) return ;
tree[t].right=tree[k].left;
tree[k].left=t;
tree[k].size=tree[t].size;
tree[t].size=tree[tree[t].left].size+tree[tree[t].right].size+;
t=k;
} void maintain(int& t,bool flag)
{
if (!t) return ;
if (!flag)
if (tree[tree[tree[t].left].left].size>tree[tree[t].right].size) rrotate(t);
else if (tree[tree[tree[t].left].right].size>tree[tree[t].right].size)
{
lrotate(tree[t].left);
rrotate(t);
} else return ;
else
if (tree[tree[tree[t].right].right].size>tree[tree[t].left].size) lrotate(t);
else if (tree[tree[tree[t].right].left].size>tree[tree[t].left].size)
{
rrotate(tree[t].right);
lrotate(t);
} else return ; maintain(tree[t].left,false);
maintain(tree[t].right,true);
maintain(t,false);
maintain(t,true);
} void insert(int& t,int v)
{
if (!t)
{
sbttail++;
tree[sbttail].key=v;
tree[sbttail].size=;
t=sbttail;
} else
{
tree[t].size++;
if (v<tree[t].key) insert(tree[t].left,v);
else insert(tree[t].right,v);
maintain(t,v>=tree[t].key);
}
} int del(int& t,int v)
{
int ret;
tree[t].size--;
if (v==tree[t].key||(v<tree[t].key&&tree[t].left==)||(v>tree[t].key&&tree[t].right==))
{
ret=tree[t].key;
if (tree[t].left==||tree[t].right==) t=tree[t].left+tree[t].right;//
else tree[t].key=del(tree[t].left,tree[t].key+);
} else
{
if (v<tree[t].key) ret=del(tree[t].left,v);
else ret=del(tree[t].right,v);
}
return ret;
} int select(int t,int k)
{
if (k==tree[tree[t].left].size+) return t;
if (k<=tree[tree[t].left].size) return select(tree[t].left,k);
else return select(tree[t].right,k--tree[tree[t].left].size);
} int main()
{
//freopen("1.txt","r",stdin);
//freopen("st.txt","w",stdout); int n,k;
while(scanf("%d%d",&n,&k)==)
{
memset(tree,,sizeof(tree));
sbttail=;
sbt=; char c;
for (int i=;i<=n;i++)
{
c=getchar();
while(c!='I'&&c!='Q')
{
c=getchar();
}
int now;
if (c=='I')
{
scanf("%d",&now);
insert(sbt,now);
} else
{
now=select(sbt,sbttail-k+);
printf("%d\n",tree[now].key);
}
}
} return ;
}