在树形选择排序中,利用锦标赛思想建立的树称为胜者树。

1、每个非终端节点存储的是左右孩子节点中的优胜者。

2、通过减少比较次数,提高效率。

3、胜者树就是一颗特殊的线段树。

一、构建树

Procedure buildmint;  复杂度 O(n)
var i,j,k:longint;
begin
  i:=N; j:=N+n-; { 其中 N 为大于等于 n 的最小 2 次方数 }
  while i> do
  begin
    k:=i;
    while k<j do
    begin
      f[k div ]:=min(f[k],f[k+]);
      k:=k+;
    end;
    if j and = then f[j div ]:=f[j];
    i:=i div ; j:=j div ;
  end;
end;

二、查询

核心思想:自下向上,奇偶缩进。

Function findmin(x,y:longint):longint;  复杂度 O(logn)
var i,j,k,r:longint;
begin
  i:=x+N-; j:=y+N-; r:=i;
  while i<=j do
  begin
    if (f[r]>f[i]) then r:=i;
    if (f[r]>f[j]) then r:=j;
    if (i and =) then inc(i);
    if (j and =) then dec(j);
    i:=i shr ; j:=j shr ;
  end; exit(f[r]);
  while r<N do
  if (f[r]=f[r*]) then r:=r* else r:=r*+;
  exit(r-N+);
end;

三、更新

核心思想:自下而上,更新父节点。

胜者树不擅长更新,因为,某个结点更新的时候,它还需要跟兄弟结点比较。更新频繁的情况,用败者树。

四、败者树

败者树是胜者树的一种变体。在败者树中,用父结点记录其左右子结点进行比赛的败者,而让胜者参加下一轮的比赛。败者树的根结点记录的是败者,需要加一个结点来记录整个比赛的胜利者。采用败者树可以简化重构的过程。

典型应用:多路归并排序,可以减少比较次数。

五、代码实现胜者树

poj2328 http://poj.org/problem?id=2823

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

#include <iostream>
#include <cmath> using namespace std; const int MX = ; int n, k;
int a[MX], minRes[MX], maxRes[MX];
int treeMin[*MX], treeMax[*MX];
int N; //构建一颗winner tree,tree用数组存储,根结点是数组的第二个结点,即A[1]
//叶子结点是原始数组。树的最左结点的下标是2的指数
void build() {
int i = N, j = N+n-;
while (i > ) {
int t = i;
while (t < j) {
treeMin[t/] = min(treeMin[t], treeMin[t+]);
treeMax[t/] = max(treeMax[t], treeMax[t+]);
t += ;
}
if ((j&) == ) {//下标j是偶数,说明这个结点是落单的
treeMin[j/] = treeMin[j];
treeMax[j/] = treeMax[j];
}
i = i>>;
j = j>>;
}
} int query(int x, int y) {
int i=x+N, j=y+N, t = i;
while (i <= j) {
if (treeMin[t] > treeMin[i]) t = i;
if (treeMin[t] > treeMin[j]) t = j;
if ((i&) == ) i++;
if ((j&) == ) j--;
i = i>>;
j = j>>;
}
//return treeMin[t];
while (t < N) {
if (treeMin[t] == treeMin[t*]) t = t*;
else t = t*+;
}
return treeMin[t];
//return t - N;
} void work() {
for (int i=; i<n-k+; i++) {
int x=N+i, y=x+k-, t=x, r=x;
while (x<=y) {
if (treeMin[t] > treeMin[x]) t = x;
if (treeMin[t] > treeMin[y]) t = y;
if (treeMax[r] < treeMax[x]) r = x;
if (treeMax[r] < treeMax[y]) r = y;
if ((x&) == ) x++;
if ((y&) == ) y--;
x = x>>;
y = y>>;
}
minRes[i] = treeMin[t];
maxRes[i] = treeMax[r];
}
} void printRes() {
for (int i=; i<n-k+; i++) {
if (i!=) cout<<" ";
cout<<minRes[i];
}
cout<<endl;
for (int i=; i<n-k+; i++) {
if (i!=) cout<<" ";
cout<< maxRes[i];
}
cout<<endl;
} int calN(int x) {
int i = ;
while (i < x) {
i = i<<;
}
return i;
} void printTree() {
cout << "min winner tree:" << endl;
for (int i=; i< n+N; i++) {
cout << treeMin[i] << " ";
}
cout << endl << "max winner tree:" << endl;
for (int i=; i<n + N; i++) {
cout << treeMax[i] << " ";
}
cout << endl;
} int main()
{
cin >> n >> k;
N = calN(n);
for (int i=; i<n; i++) {
cin >> a[i];
treeMax[N+i] = treeMin[N+i] = a[i];
} build(); //printTree(); work();
printRes(); //cout<<"query:"<< query(0, 2)<<endl; //cout << "Hello world!" << endl;
return ;
}

winner tree 胜者树的更多相关文章

  1. Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)

    在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...

  2. 页面设计--Tree目录树

    Tree目录树控件属性: 根据数据集合来配置相应的信息 加载模式有自动加载.自定加载 web中显示效果图:

  3. [转] Splay Tree(伸展树)

    好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...

  4. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  5. 【数据结构】B-Tree, B+Tree, B*树介绍 转

    [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...

  6. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  7. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  8. C++胜者树

    #include <iostream> #define MAX_VALUE 0x7fffffff using namespace std; //在这里我先反思一下.不知道怎么搞的,这个算法 ...

  9. MYSQL的B+Tree索引树高度如何计算

    前一段被问到一个平时没有关注到有关于MYSQL索引相关的问题点,被问到一个表有3000万记录,假如有一列占8位字节的字段,根据这一列建索引的话索引树的高度是多少? 这一问当时就被问蒙了,平时这也只关注 ...

随机推荐

  1. NEC学习 ---- 布局 -两列, 左侧定宽,右侧自适应

    CSS代码:以下两处代码是NEC中CSS初始化样式和功能性样式.今后的NEC研究中,默认这两处是引用的. /* 这是CSS reset 代码 --- 初始化样式 */ /* reset */ html ...

  2. CentOS_6.5安装Nginx+PHP+MySQL

    #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Ch ...

  3. android source compiler

  4. CDH商业版本的搭建(hadoop+hive+sqoop)

    一:准备工作 1.步骤 1)hadoop ->下载解压 ->修改配置文件 ->hadoop-env JAVA_HOME ->core-site fs.defaultFS had ...

  5. Magento去掉价格的小数点

    magento的默认情况,价格后面是有小数点的,我们来看下如何正确的来去掉小数点. 1.复制如下路径的文件 app/code/core/Mage/Directory/Model/Currency.ph ...

  6. Subset---poj3977(折半枚举+二分查找)

    题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]| ...

  7. iOS 拉伸图片

    UIEdgeInsets:四个参数,会填入上左下右几个值,这几个值,代表着距离边界的这几个点描绘的区域,是不会拉伸的.所以,到时候,计算清楚这几个值就可以了.

  8. JS-010-覆盖率测试工具 JSCoverage 初识

    在日常的 js 脚本语言开发过程中,在开发攻城狮进行日常的单元测试和测试攻城狮日常的测试过程中,js 代码的覆盖率是白盒测试的一个重要的考量标准.前些天,在无意中看到了一个 js 覆盖率测试统计工具 ...

  9. Java学习-003-JDK、JRE、JVM简介

    此文主要对 JDK.JRE.JVM进行简单的介绍,给各位亲们一个参考.若有不足之处,敬请各位大神指正,不胜感激! 一.基本概念 JDK(Java Development Kit:Java 开发工具包) ...

  10. [转]android自动弹出软键盘(输入键盘)

    转自:http://www.devdiv.com/home.php?mod=space&uid=65729&do=blog&id=11847 很多应用中对于一个界面比如进入搜索 ...