提供动态更新数据。第实时QK大量的值什么?

使用AVL统计数据结构做,比较先进的数据结构的内容。

不知道给出的数据为准值是否有反复。下面的程序是因为我能够处理重复数据出现的情况下,。

了repeat的信息,能够知道出现了当前数组多少次。

主要是知道怎样维护这些数据和怎样查询,维护数据的函数是pushUp,查询函数是selectKth。

其它就是一般的AVL操作。

个人认为我的AVL写的还算蛮清晰的,有须要的參考一下。

#include <stdio.h>

inline int max(int a, int b) { return a > b? a : b; }
inline int min(int a, int b) { return a < b? a : b; } struct Node
{
int key, h, size, repeat; //repeat for record the repeated key times
Node *left, *right;
explicit Node(int val = 0) : left(NULL), right(NULL),
key(val), repeat(1), h(1), size(1){}
}; inline int getHeight(Node *n)
{
if (!n) return 0;
return n->h;
} inline int getSize(Node *n)
{
if (!n) return 0;
return n->size;
} inline int getBalance(Node *n)
{
if (!n) return 0;
return getHeight(n->left) - getHeight(n->right);
} inline void pushUp(Node *n)
{
if (!n) return ;
n->size = getSize(n->left) + getSize(n->right) + n->repeat;
n->h = max(getHeight(n->left), getHeight(n->right)) + 1;
} inline Node *leftRotate(Node *x)
{
Node *y = x->right;
x->right = y->left;
y->left = x; pushUp(x);
pushUp(y);
return y;
} inline Node *rightRotate(Node *x)
{
Node *y = x->left;
x->left = y->right;
y->right = x; pushUp(x);
pushUp(y);
return y;
} inline Node *balanceNode(Node *n)
{
if (!n) return n; int balance = getBalance(n);
if (balance > 1)
{
if (getBalance(n->left) < 0) n->left = leftRotate(n->left);
n = rightRotate(n);
}
else if (balance < -1)
{
if (getBalance(n->right) > 0) n->right = rightRotate(n->right);
n = leftRotate(n);
}
return n;
} Node *insert(Node *rt, int val)
{
if (!rt) return new Node(val); if (rt->key == val) rt->repeat++;
else if (rt->key < val) rt->left = insert(rt->left, val);
else rt->right = insert(rt->right, val); pushUp(rt);
return balanceNode(rt);
} int selectKth(Node *rt, int k)
{
int lSize = getSize(rt->left);
if (k <= lSize) return selectKth(rt->left, k);
else if (lSize + rt->repeat < k)
return selectKth(rt->right, k - lSize - rt->repeat);
return rt->key; // lSize < k <= lSize+rt->repeat
} void deleteTree(Node *rt)
{
if (rt)
{
if (rt->left) deleteTree(rt->left);
if (rt->right) deleteTree(rt->right);
delete rt; rt = NULL;
}
} int main()
{
int n, k, val;
char c;
while (scanf("%d %d", &n, &k) != EOF)
{
Node *tree = NULL;
for (int i = 0; i < n; i++)
{
getchar();// get rid of '\n'
c = getchar();
if ('I' == c)
{
scanf("%d", &val);
tree = insert(tree, val);
}
else
{
printf("%d\n", selectKth(tree, k));
}
}
deleteTree(tree);
}
return 0;
}

版权声明:笔者心脏靖,景空间地址:http://blog.csdn.net/kenden23/。可能不会在未经作者同意转载。

HDU 4006 The kth great number AVL解的更多相关文章

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

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

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

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

  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(优先队列)

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

  6. hdu 4006 The kth great number

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

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

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

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

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

  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. android:强大的图像下载和缓存库Picasso

    1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...

  2. Swing动画之游戏角色

    一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...

  3. Cocos2d-x 单点触摸--让我们用手指动起来的精灵

    转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25656673 效果图: CCTouch类装载了触摸点的信息.包含触摸点的横纵坐标值和触 ...

  4. 设计模式模式游客(Visitor)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...

  5. RH133读书笔记(9)-Lab 9 Installation and System-Initialization

    Lab 9 Installation and System-Initialization Goal: Successfully install Red Hat Enterprise Linux. Sy ...

  6. 每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询

    前言: 前一段时间一直都比較忙.没有什么时间进行总结,刚好节前项目上线.同一时候趁着放假能够好好的对之前遇到的一些问题进行总结. 主要内容有使用SharePoint服务端对象模型进行查询.为Share ...

  7. 在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败

    原文:在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败 问题: 在SQL Server Management Studio中可以运行作业但是用T-SQ ...

  8. Android 通过应用程序来设置系统的日期和时间中的

    Android 通过应用程序来设置系统的日期和时间中的 android 2.3 android 4.0 测试可行,刚需ROOT权限. import java.io.DataOutputStream; ...

  9. bash no such file or directory in ubuntu 1404

    我在我的今天macbook pro retina 里面安装的虚拟机ubuntu 1404. 当我试图执行cadence ncverilog时间.ubuntu终端错误"bash no such ...

  10. Java使用串行编程操作继电器

    首先,我们必须建立一个良好的环境,那是,jdk并且tomcat.如果它不必须是web装了! 还有就是配置,也就是默认的comm.jar ,javax.comm.properties , win32co ...