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 ;
}

HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)的更多相关文章

  1. hdu 4006 The kth great number (优先队列)

    /********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...

  2. hdu 4006 The kth great number(优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...

  3. HDU 4006 The kth great number (优先队列)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  4. HDU - 4006 The kth great number multiset应用(找第k大值)

    The kth great number Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming ...

  5. HDU 4006 The kth great number(multiset(或者)优先队列)

    题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...

  6. HDU 4006 The kth great number【优先队列】

    题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...

  7. hdu 4006 The kth great number

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 思路:利用优先队列的性质,将数据存入后会自动对数据进行排序 #include<stdlib ...

  8. HDU 4006 The kth great number AVL解

    提供动态更新数据.第实时QK大量的值什么? 使用AVL统计数据结构做,比较先进的数据结构的内容. 不知道给出的数据为准值是否有反复.下面的程序是因为我能够处理重复数据出现的情况下,. 了repeat的 ...

  9. hdoj 4006 The kth great number【优先队列】

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

随机推荐

  1. LeetCode OJ 95. Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  2. 今天开通博客,记录我作为一个小菜鸟在iOS学习中的点点滴滴

    一直以来都是默默的关注各位同仁,没有为网站作什么贡献. 现在借开始学习iOS开发的这个机会开博,集中于介绍这个过程,激励我自己. 谢谢大家!

  3. PHP数组函数试题

    使用Ctrl+A查看答案 1.将数组的键名全部转换成小写和大写的函数是什么?答:array_change_key_case($array [,CASE_LOWER|CASE_UPPER]) 2.创建一 ...

  4. 关于安卓HTTP请求用HttpUrlConnection还是HttpClient好

    安卓和JAVA应用开发少不了要提交HTTP请求,而基本上目前有两个实现方式:HttpUrlConnection(即URL.openConnection)和HttpClient. 网上不少人都认为Htt ...

  5. linux学习的哲学层面的思考-架构

    参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...

  6. 转 : net use的使用

    老是忘了 net use 怎么样,今天在网上找一篇,贴在这,感谢原作者分享.     1 查看远程主机的共享资源(但看不到默认共享) net view \\IP 2向远程主机复制文件 copy \路径 ...

  7. Android 项目开发

    可以使用mapview.getMapCenter()获取当前可视范围中心点的坐标,然后计算出数据库中的点与中心点的距离值,如果该距离在触发显示的范围内(比如100米),就显示该点到地图上.百度地图的S ...

  8. VMI

    在虚拟机外部监控虚拟机内部运行状态的方法被称为虚拟机自省(Virtual Machine Introspection,VMI).VMI允许特权域查看非特权域的运行状态,并能获得被监控虚拟机运行状况相关 ...

  9. IOS 使用动态库(dylib)和动态加载framework

    在iphone上使用动态库的多为dylib文件,这些文件使用标准的dlopen方式来使用是可以的.那相同的在使用framework文件也可以当做动态库的方式来动态加载,这样就可以比较自由的使用appl ...

  10. DHCP详细工作过程(转)

    DHCP客户端通过和DHCP服务器的交互通讯以获得IP地址租约.为了从DHCP服务器获得一个IP地址,在标准情况下DHCP客户端和DHCP服务器之间会进行四次通讯.DHCP协议通讯使用端口UDP 67 ...