码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-08 21:25:06      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:acm   数据结构   

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

这道可以直接就用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. 数据域: 
  5. 值域key,左孩子left,右孩子right,保持平衡的size; 
  6.  
  7. 性质: 
  8. 每棵子树的大小不小于其兄弟的子树大小; 
  9.  
  10. 插入: 
  11. 插入算法先简单插入节点,然后调用一个维护过程以保持性质; 
  12.  
  13. 删除: 
  14. 删除操作与普通维护size域的二叉查找树相同; 
  15.  
  16. 最大值和最小值: 
  17. 由于SBT本身已经维护了size域; 
  18. 所以只需用Select(T,1)来求最大值; 
  19. Select(T,T.size)求最小值; 
  20. 其中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讲的还不错,自己还是要多看几遍,还没完全弄懂
这个题目看到别人用了线段树来做;线段树每个节点保存当前编号的数出现的次数。每次查询从小到大第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

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

标签:acm   数据结构   

原文地址:http://blog.csdn.net/whjkm/article/details/38436681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!