提供动态更新数据。第实时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. Node.js Tools for Visual Studio

    https://www.visualstudio.com/en-us/features/node-js-vs.aspx

  2. c++日历改进版

    #include<iostream> # include<fstream> #include<time.h> #include<string> #inc ...

  3. Apple Watch 1.0 开发介绍 2.1 WatchKit Apps UI要点

    实现app的开始是定义storyboard场景.每个场景定义了app的一部分界面.可以为不同的尺寸自定义场景. 组装storyboard界面 WatchKit app和iOS app的布局模式不同.组 ...

  4. 概率统计(DP)

    问题叙述性说明 生成n个月∈[a,b]随机整数.并且将它们输出到x概率. 输入格式 输入线跟四个整数n.a,b,x,用空格分隔. 输出格式 输出一行包括一个小数位和为x的概率.小数点后保留四位小数 例 ...

  5. AsyncTask測试多任务

    本人进行过模拟測试,发现AsyncTask并不适合多任务,以及长期的异步任务,由于每次仅仅能执行一个AsyncTask,假设执行多个其他任务将会等待 以下通过一个代码样例和日志打印得到证实. 以下扩展 ...

  6. Mobile Services 提交批量数据

    Mobile Services批量提交数据,參考了文章:Inserting multiple items at once in Azure Mobile Services.里面事实上已经介绍得比較清楚 ...

  7. Afinal载入网络图片及下载文件用法

    Afinal高速开发框架使用起来很方便.以下将解说怎样利用Afinal载入网络图片及下载文件: 先看效果图: 注意:使用Afinal前需加入Afinal的jar,能够在这里下载:http://down ...

  8. android JNI 简单demo(2)它JNI demo 写

    android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...

  9. ASP.NET开发规范:OWIN

    ASP.NET开发规范:OWIN 今天投简历 准备面试了... 本节目录: OWIN简介 OWIN规范 Katana Hello World(3种Host) 自定义Middleware OWIN简介 ...

  10. Struts2_1_struts2建立一个执行环境

    1)最低需要进口jar包: commons-fileupload-1.2.1.jar.commons-logging-1.0.4.jar. freemarker-2.3.15.jar.ognl-2.7 ...