HDU 4006 The kth great number AVL解
提供动态更新数据。第实时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解的更多相关文章
- hdu 4006 The kth great number (优先队列)
/********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...
- 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 ...
- HDU 4006 The kth great number (优先队列)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- 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 ...
- hdu 4006 The kth great number(优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...
- hdu 4006 The kth great number
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 思路:利用优先队列的性质,将数据存入后会自动对数据进行排序 #include<stdlib ...
- HDU 4006 The kth great number(multiset(或者)优先队列)
题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...
- HDU 4006 The kth great number【优先队列】
题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...
- hdoj 4006 The kth great number【优先队列】
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
随机推荐
- cocos2d-html5
cocos2d-html5 简单的混乱 在正常情况下,build设置完毕后 跑ant编 变更后cocos2d.js 简单直观so easy 只需要上传cocos2d.js/game.js/index文 ...
- UVA 1291 Dance Dance Revolution(DP)
意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...
- CI-持续集成(2)-软件工业“流水线”技术实现(转)
1 概述 持续集成(Continuous Integration)是一种软件开发实践.在本系列文章的前一章节已经对其背景及理论体系进行了介绍.本小节则承接前面提出的理论构想进行具体的技术实现. & ...
- 恶意软件"跨平台" 小心钱包很受伤
什么是跨平台攻击? 举例来说.就像网络诈骗犯为了避开电子商务平台的监控.会在微博上发消息.百度上撒网,腾讯上联系,最后在淘宝上交易.这样的跨平台操作的模式会大大添加犯罪过程监控和取证的难度.而跨平台攻 ...
- hud 1312 Red and Black
题目: 链接:pid=1312">点击打开链接 题意: DFS搜索 算法: dfs 思路: 简单题 代码: #include<iostream> #include<c ...
- Thread thread2 = new Thread()
Thread thread2 = new Thread() { @Override public void run() { test.function(); } }; thread1.start(); ...
- Android网络通信Volley框架源代码浅析(三)
尊重原创 http://write.blog.csdn.net/postedit/26002961 通过前面浅析(一)和浅析(二)的分析.相信大家对于Volley有了初步的认识,可是假设想更深入的理解 ...
- 使用C#在Windows应用商店程序中获取CPU信息
using Windows.Devices.Enumeration; string guidStr="{97FADB10-4E33-40AE-359C-8BEF029DBDD0}" ...
- 全自动Web后门扫描(转)
阅读目录 工具介绍 使用方法 工具介绍 这是一款全自动Web后门查杀工具,基于Python开发 某些较新的后门可能会查杀失败 规则列表来自seay博客 回到顶部 使用方法 1.按恶意代码查杀: pyt ...
- C++在设计和使用智能指针
为一个C++用户的.使用指针可以算的上是常态,但在使用过程中.多的时间,可能是由于new要么malloc对象,上次忘记的释放结束(我会犯这样一个错误).内存泄露. 而此时智能指针可能能够帮助我去解决问 ...