zoukankan      html  css  js  c++  java
  • 败者树

    //参考链接:http://blog.chinaunix.net/uid-25324849-id-2182916.html
    #include <iostream>
    
    using namespace std;
    
    const int N = 10000000;
    const int FILE_NUM = 5;
    const int MAX_PART = 1000000;
    const int MIN = -1;     //最小值, 必须比要排序数字的最小值要小,否则出错
    const int MAX = N + 1;  //最大值 ,必须比要排序数字的最大值要大,否则出错
    
    //调整
    void adjust(int ls[], int data[], int s)
    {
        int t = (s + FILE_NUM)/2;
        while (t)
        {
            if (data[s] > data[ls[t]])
            {
                int temp = s;
                s = ls[t];
                ls[t] = temp;
            }
                t /= 2;
        }
        ls[0] = s;
    }
    
    void create_loser_tree(int ls[], int data[])
    {
        data[FILE_NUM] = MIN;
        for (int i = 0; i < FILE_NUM; i++)
        {
            ls[i] = FILE_NUM;
        }
        for (int i = FILE_NUM - 1; i >= 0; i--)
        {
            adjust(ls, data, i);
        }
    }
    
    void merge_sort_by_losertree(int *data)
    {
        int ls[FILE_NUM];             //存放败者索引的节点
    
    
        create_loser_tree(ls, data); //创建败者树
           for(int i = 0;i<FILE_NUM;i++)
                cout << data[i] << " ";
          cout << "    min:" ;
          cout << data[ls[0]] << endl;
          data[4] = 3;
          adjust(ls, data, ls[0]);
           for(int i = 0;i<FILE_NUM;i++)
                cout << data[i] << " ";
          cout << "     min:" ;
          cout << data[ls[0]] << endl;
    }
    
    int main()
    {
           int data[10+1] = {13,5,6,4,99,9,12,17,13,4};
        merge_sort_by_losertree(data);
        return 0;
    }
  • 相关阅读:
    基础数据结构总结
    图论总结
    【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线
    【bzoj1015】星球大战starwar
    NOIP2012摆花
    最勇敢的机器人
    【bzoj1056】排名系统
    图的第k短路
    【bzoj1455】罗马游戏
    ti
  • 原文地址:https://www.cnblogs.com/tractorman/p/3944398.html
Copyright © 2011-2022 走看看