zoukankan      html  css  js  c++  java
  • [存档]Young Tableau

    you know...

    #include <cassert>
    #include <algorithm>
    using namespace std;
    
    const int INF = 1111111111;
    const int M = 1111;
    const int N = 1111;
    
    //minimum young_tableau
    struct young_tableau {
        int a[M][N];
    
        young_tableau() {
            for (int i = 0; i < M; ++i)
                for (int j = 0; j < N; ++j)
                    a[i][j] = INF;
        }
    
        bool empty() {
            return !(a[0][0]<INF);
        }
    
        bool full() {
            return a[M-1][N-1]<INF;
        }
    
        //O(1)
        int top() {
            assert(!empty());
            return a[0][0];
        }
    
        void sink(int i, int j) {
            int mi = i, mj = j;
            if (i<M-1 && a[i+1][j]<a[mi][mj]) mi = i+1, mj = j;
            if (j<N-1 && a[i][j+1]<a[mi][mj]) mi = i, mj = j+1;
            if (mi!=i || mj!=j) {
                swap(a[i][j], a[mi][mj]);
                sink(mi, mj);
            }
        }
    
        //O(M+N)
        void pop() {
            assert(!empty());
            a[0][0] = INF;
            sink(0, 0);
        }
    
        void rise(int i, int j) {
            int mi = i, mj = j;
            if (i>0 && a[mi][mj]<a[i-1][j]) mi = i-1, mj = j;
            if (j>0 && a[mi][mj]<a[i][j-1]) mi = i, mj = j-1;
            if (mi!=i || mj!=j) {
                swap(a[i][j], a[mi][mj]);
                rise(mi, mj);
            }
        }
    
        //O(M+N)
        void push(int key) {
            assert(!full());
            a[M-1][N-1] = key;
            rise(M-1, N-1);
        }
    
        //O(M+N)
        //(M, N) for not found
        pair<int, int> lower_bound(int key) {
            pair<int, int> res = make_pair(M, N);
            int i = 0, j = N-1;
            while (i<M && j>=0) {
                if (a[i][j]<key) i++;
                else res = make_pair(i, j--);
            }
            return i<M&&j>=0 ? res : make_pair(M, N);
        }
    
        pair<int, int> find(int key) {
            pair<int, int> res = lower_bound(key);
            if (res==make_pair(M, N) || key<a[res.first][res.second])
                return make_pair(M, N);
            else
                return res;
        }
    };
  • 相关阅读:
    SQL EXPLAIN优化详解
    2019数据智能算法大赛赛后复盘
    K-D树详解
    点云配准相关
    pandas速查手册(中文版)
    Windows CMD命令大全
    获取时间戳
    SQL 字符串转成临时表
    delphi cxgrid明细新增第三行的报错【Key violation】
    cxgrid列的Properties(cxPopupEditPopup)的关闭方法
  • 原文地址:https://www.cnblogs.com/jffifa/p/2740109.html
Copyright © 2011-2022 走看看