hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number

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

Total Submission(s): 6637    Accepted Submission(s): 2671

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

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

Sample Output

1
2
3

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).

Source

The
36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

这道可以直接就用STL的优先队列水过,最近要熟悉stl里面的操作和用法,用这些题练练手,还是不错的,这道题要注意的是用stl的优先队列时,元素的比较规则默认是按元素值的大小从大到小排序;这道题用优先队列来做的话,直接是维护一个k长度的优先队列,要把元素值大小从小到大排序,它的第k大的值就是队首元素,所以每次的查询就输出队首元素就行了;这里就需要重载操作符来实现它的排序

下面是用stl写的代码;

#include <cstdio>
#include <queue>
using namespace std;
int main()
{
    int n,k,m;
    char s[5];
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        priority_queue<int, vector<int>, greater<int> > q;//这里就是对优先队列进行排序
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            if(s[0]=='I')
            {
                scanf("%d",&m);
                if(q.size()<k)
                    q.push(m);//入队
                else if(m>q.top())
                {
                    q.pop();//出队
                    q.push(m);
                }
            }
            else
            {
                printf("%d\n",q.top());//输出
            }
        }
    }
    return 0;
}

这道题还可以用其他方法做,在网上还看到了其他方法,用SBT也可以做,也是一种平衡二叉树,这里学习一下这种写法,可以用作以后的模板;

  1. 数据结构:
  2. SBT(Size Balanced Tree),又称傻逼树;
  3. 数据域:
  4. 值域key,左孩子left,右孩子right,保持平衡的size;
  5. 性质:
  6. 每棵子树的大小不小于其兄弟的子树大小;
  7. 插入:
  8. 插入算法先简单插入节点,然后调用一个维护过程以保持性质;
  9. 删除:
  10. 删除操作与普通维护size域的二叉查找树相同;
  11. 最大值和最小值:
  12. 由于SBT本身已经维护了size域;
  13. 所以只需用Select(T,1)来求最大值;
  14. Select(T,T.size)求最小值;
  15. 其中Select(T,k)函数返回树T在第k位置上的节点值;

下面是ac的代码;参考别人的;sbt还是有点不清楚啊;

#include <stdio.h>
#include <string.h>
#define MAX 1000010
int n,m;
struct SBT {//结构体

    int left,right,size,key;
    void Init() {

        left = right = 0;
        size = 1;
    }
}a[MAX];
int tot,root;
void left_rotate(int &t) {//左旋转
    int k = a[t].right;
    a[t].right = a[k].left;
    a[k].left = t;
    a[k].size = a[t].size;
    a[t].size = a[a[t].left].size + a[a[t].right].size + 1;
    t = k;
}
void right_rotate(int &t) {//右旋转

    int k = a[t].left;
    a[t].left = a[k].right;
    a[k].right = t;
    a[k].size = a[t].size;
    a[t].size = a[a[t].left].size + a[a[t].right].size + 1;
    t = k;
}
void maintain(int &t,int flag) {//维护
    if (flag == 0) {

        if (a[a[a[t].left].left].size > a[a[t].right].size)
            right_rotate(t);
        else if (a[a[a[t].left].right].size > a[a[t].right].size)
            left_rotate(a[t].left),right_rotate(t);
        else return;
    }
    else {

        if (a[a[a[t].right].right].size > a[a[t].left].size)
            left_rotate(t);
        else if (a[a[a[t].right].left].size > a[a[t].left].size)
            right_rotate(a[t].right),left_rotate(t);
        else return;
    }
    maintain(a[t].left,0);
    maintain(a[t].right,1);
    maintain(t,0);
    maintain(t,1);
}
void insert(int &t,int v) {//插入
    if (t == 0)
        t = ++tot,a[t].Init(),a[t].key = v;
    else {

        a[t].size++;
        if (v < a[t].key)
            insert(a[t].left,v);
        else insert(a[t].right,v);
        maintain(t,v>=a[t].key);
    }
}
int del(int &t,int v) {//删除

    if (!t) return 0;
    a[t].size--;

    if (v == a[t].key || v < a[t].key && !a[t].left
        || v > a[t].key && !a[t].right) {

        if (a[t].left && a[t].right) {

            int p = del(a[t].left,v+1);
            a[t].key = a[p].key;
            return p;
        }
        else {

            int p = t;
            t = a[t].left + a[t].right;
            return p;
        }
    }
    else return del(v<a[t].key?a[t].left:a[t].right,v);
}
int find(int t,int k) {//查找

    if (k <= a[a[t].left].size)
        return find(a[t].left,k);
    if (k > a[a[t].left].size + 1)
        return find(a[t].right,k-a[a[t].left].size-1);
    return a[t].key;
}
int main()
{
    int i,j,k;
    while (scanf("%d%d",&n,&m) != EOF) {

        tot = root = 0;
        char ope[10];
        while (n--) {

            scanf("%s",ope);
            if (ope[0] == 'I') {

                scanf("%d",&k);
                insert(root,k);
            }
            else printf("%d\n",find(root,a[root].size+1-m));
        }
    }
}

http://www.nocow.cn/index.php/Size_Balanced_Tree这里面SBT讲的还不错,自己还是要多看几遍,还没完全弄懂

http://blog.csdn.net/jarily/article/details/8679236 这个博客也是讲SBT的

http://blog.csdn.net/acceptedxukai/article/details/6921334

这个题目看到别人用了线段树来做;线段树每个节点保存当前编号的数出现的次数。每次查询从小到大第N-K+1个数(N为当前数的总数)。

贴上别人的代码;学习一下;

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define MAXN 1001000
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
int n,k;

int node[MAXN<<2];

void push_up(int root)
{
	node[root]=node[root<<1]+node[root<<1|1];
}

void build(int l,int r,int root)
{
	if(l==r)
	{
		node[root]=0;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	push_up(root);
}

void update(int n,int l,int r,int root)
{
	if(l==r)
	{
		node[root]++;
		return ;
	}
	int m=(l+r)>>1;
	if(n<=m)
		update(n,lson);
	else
		update(n,rson);
	push_up(root);
}

int query(int p,int l,int r,int root)
{
	if(l==r)
	{
		return l;
	}
	int m=(l+r)>>1;
	int ret;
	if(p<=node[root<<1])
		ret=query(p,lson);
	else
		ret=query(p-node[root<<1],rson);
	return ret;
}

void solve()
{
	char a[5];
	int ff;
	build(1,MAXN,1);
	int counts=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",a);
		if(a[0]=='I')
		{
		scanf("%d",&ff);
		update(ff,1,MAXN,1);
		counts++;
		}
		else
		printf("%d\n",query(counts-k+1,1,MAXN,1));
	}

}

int main()
{
	while(scanf("%d%d",&n,&k)!=EOF)
		solve();
	return 0;
}

还有人用了treap树,那个和SBT类似;

这道题主要还是练一下最小堆;

对最小堆还不是很熟悉,还是要多练;可以当做自己的模板来使用

#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
int heap[1000010],size;
void down(int r)//往下交换
{
   while(r<size)
   {
       int son=r*2;
       if(son+1<=size)
        son=heap[son]>heap[son+1]?son+1:son;
       if(son<=size && heap[r]>heap[son])  swap(heap[r],heap[son]);
       else return;
       r=son;
   }
}
void up(int r)//往上交换,查询
{
    while(r!=1)
    {
        if(heap[r]<heap[r/2]) swap(heap[r],heap[r/2]);
        else break;
        r>>=1;
    }
}
void heap_push(int k)//入队
{
    heap[++size]=k;
    up(size);
}
void heap_pop()//出队
{
    heap[1]=heap[size--];
    down(1);
}
int main()
{
    int n,k,m;
    char s[5];
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        size=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            if(s[0]=='I')
            {
                scanf("%d",&m);
                heap_push(m);
                if(size>k) heap_pop();
            }
            else
            {
                printf("%d\n",heap[1]);
            }
        }
    }
    return 0;
}

用堆来模拟优先队列。

hdu 4006 The kth great number (优先队列+STB+最小堆),布布扣,bubuko.com

时间: 2024-10-08 20:51:43

hdu 4006 The kth great number (优先队列+STB+最小堆)的相关文章

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

1 /********************************************************** 2 题目: The kth great number(HDU 4006) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4006 4 算法: 优先队列 5 ************************************************************/ 6 #include<cstdio> 7 #

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

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6982    Accepted Submission(s): 2837 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number(优先队列&#183;第K大数)

题意  动态查询第K大的数 用小数在前优先队列维护K个数  每要插入一个数时 若这个数小于队首元素那么就不用插入了  否则队首元素出队  这个数入队  每次询问只用输出队首元素就行了 #include<cstdio> #include<queue> using namespace std; int main() { int n, a, k; char op[5]; while(~scanf("%d%d", &n, &k)) { priority_

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

题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相同的数没直接放在相同的现有的数据后面的 #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; //#define IN freopen(

HDU 4006 The kth great number (基本算法-水题)

The kth great number 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 mu

HDU 4006 The kth great number AVL解法

给出动态更新数据,实时问第K个大的数值是什么? 利用AVL数据结构做的一个统计数,比较高级的数据结构内容了. 不知道题目给出的数据值是否有重复,因为我下面的程序是可以处理出现数据重复的情况的. 其中的奥妙是增加了repeat的信息,可以知道出现了当前数组多少次. 主要是知道如何维护这些数据和如何查询,维护数据的函数是pushUp,查询函数是selectKth. 其他就是一般的AVL操作.个人觉得我的AVL写的还算蛮清晰的,有需要的参考一下. #include <stdio.h> inline

HDU 4006: The kth great number

The kth great number ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; while (cin >> n >> k) { char ch; int t; multiset<int>s; while (

hdoj 4006 The kth great number(优先队列)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9415    Accepted Submission(s): 3756 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round

hdu 4006 The kth great number,set,priority_queue

The kth great number 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 feel