首页 > 其他分享 >Binary Heap

Binary Heap

时间:2024-03-12 20:36:18浏览次数:26  
标签:std Binary cont idx comp Heap auto BinaryHeap

Binary Heap

一个基于 concept 的二叉堆板子实现。

template <typename Ty, typename Compare, typename Container = std::vector<Ty>>
  requires requires(Compare comp, Ty a, Ty b) {
    std::is_same_v<bool, decltype(comp(a, b))>;
  } &&
           requires(Container cont, Ty value) {
             std::is_same_v<decltype(cont[0]), Ty>;
             std::is_same_v<decltype(cont.front()), Ty>;
             std::is_same_v<decltype(cont.back()), Ty>;
             cont.push_back(value);
             cont.emplace_back(value);
             cont.emplace_back(std::move(value));
             cont.pop_back();
           } && requires(const Container cont) {
             std::is_same_v<decltype(cont.front()), Ty>;
             std::is_same_v<decltype(cont.back()), Ty>;
             std::is_same_v<decltype(cont.size()), std::size_t>;
             std::is_same_v<decltype(cont.empty()), bool>;
           }
class BinaryHeap {
public:
  BinaryHeap() : cont_(), comp_() {}

  explicit BinaryHeap(const Compare &comp) : cont_(), comp_(comp) {}

  explicit BinaryHeap(Compare comp) : cont_(), comp_(std::move(comp)) {}

  BinaryHeap(const BinaryHeap &other)
      : cont_(other.cont_), comp_(other.comp_) {}

  BinaryHeap(BinaryHeap &&other)
      : cont_(std::move(other.cont_)), comp_(std::move(other.comp_)) {}

  template <typename Comp, typename Cont>
  explicit BinaryHeap(Comp &&comp, Cont &&cont)
      : cont_(std::forward<Cont>(cont)), comp_(std::forward<Comp>(comp)) {
    for (int i = static_cast<int>(this->cont_.size()) - 1; i >= 0; i--) {
      this->Down(i);
    }
  }

  auto operator=(BinaryHeap other) -> BinaryHeap & {
    this->Swap(other);
    return *this;
  }

  ~BinaryHeap() noexcept = default;

  auto Swap(BinaryHeap &other) noexcept -> void {
    std::swap(this->cont_, other.cont_);
    std::swap(this->comp_, other.comp_);
  }

public:
  auto Size() const noexcept -> std::size_t { return this->cont_.size(); }

  auto Empty() const noexcept -> bool { return this->cont_.empty(); }

  auto Push(Ty value) -> void {
    this->cont_.emplace_back(std::move(value));
    this->Up(this->Size() - 1);
  }

  template <typename... Ts>
    requires requires(Ts &&...args) { Ty(std::forward<Ts>(args)...); }
  auto Emplace(Ts &&...args) -> void {
    this->cont_.emplace_back(std::forward<Ts>(args)...);
    this->Up(this->Size() - 1);
  }

  auto Top() const -> const Ty & { return this->cont_[0]; }

  auto Top() -> Ty & { return this->cont_[0]; }

  auto Pop() -> void {
    std::swap(this->cont_.front(), this->cont_.back());
    this->cont_.pop_back();
    this->Down(0);
  }

private:
  auto Up(std::size_t idx) -> void {
    while (idx > 0 &&
           this->comp_(this->cont_[idx], this->cont_[(idx - 1) / 2])) {
      std::swap(this->cont_[(idx - 1) / 2], this->cont_[idx]);
      idx = (idx - 1) / 2;
    }
  }

  auto Down(std::size_t idx) -> void {
    while (idx * 2 + 1 < this->Size()) {
      auto t = idx * 2 + 1;
      if (t + 1 < this->Size() &&
          this->comp_(this->cont_[t + 1], this->cont_[t])) {
        t += 1;
      }
      if (this->comp_(this->cont_[idx], this->cont_[t])) {
        break;
      }
      std::swap(this->cont_[idx], this->cont_[t]);
      idx = t;
    }
  }

private:
  Container cont_;
  Compare comp_;
};

template <typename Comp, typename Cont>
BinaryHeap(Comp &&, Cont &&)
    -> BinaryHeap<typename Cont::value_type, Comp, Cont>;

标签:std,Binary,cont,idx,comp,Heap,auto,BinaryHeap
From: https://www.cnblogs.com/FlandreScarlet/p/18069160

相关文章

  • B. Quasi Binary
    It'sasimpleproblemoncodeforces.wetraversethethroughthestringn,ifweencoutera'1',weaddanewstringtoansandsetstoptofalse.Otherwise,westoptheloop.Oncewefind1,wethenappendeither'1'or'0�......
  • Binary Tree Maximum Path Sum
    SourceGivenabinarytree,findthemaximumpathsum.Thepathmaystartandendatanynodeinthetree.ExampleGiventhebelowbinarytree,1/\23Return6.题解1-递归中仅返回子树路径长度题目很短,要求返回最大路径和。咋看一下......
  • CF1066E Binary Numbers AND Sum 题解
    分析因为\(a\)是一直没有改变的,移动的只有\(b\),所以从\(a\)的每一位的贡献入手。对于\(a\)中的从低到高第\(i\)位,其对应的十进制值是\(a_{n-i+1}\times2^{i-1}\)。注意到\(b\)是每次右移一位的,所以在\(b\)中能与\(a_{n-i+1}\)匹配的都是在下标区间\([1,m-i+1]......
  • Go 100 mistakes - #95: Not understanding stack vs. heap
       ......
  • [LeetCode] 2864. Maximum Odd Binary Number
    Youaregivenabinarystringsthatcontainsatleastone'1'.Youhavetorearrangethebitsinsuchawaythattheresultingbinarynumberisthemaximumoddbinarynumberthatcanbecreatedfromthiscombination.Returnastringrepresentin......
  • [LeetCode] 2583. Kth Largest Sum in a Binary Tree
    Youaregiventherootofabinarytreeandapositiveintegerk.Thelevelsuminthetreeisthesumofthevaluesofthenodesthatareonthesamelevel.Returnthekthlargestlevelsuminthetree(notnecessarilydistinct).Iftherearefewerthan......
  • [USACO11NOV]Binary Sudoku G 题解
    定义一个\(3\times3\)的表格\(a\),表示每个小九宫格内1的个数的奇偶状态。再定义两个长为\(9\)的数组\(c0,c1\),表示每行每列上1的个数的奇偶状态。当\(d_{i,j}\)取反时,会将\(a_{[\frac{i}{3}],[\frac{j}{3}]},c0_i,c1_j\)各取反一次。要将\(a_{i,j}\)全部变为0......
  • Nginx添加第三方模块,出现“is not binary compatible in”错误的解决方案
    动态编译好第三方模块:ngx_http_ts_module.so 检测nignx配置,异常sudo/usr/local/openresty/nginx/sbin/nginx-tnginx:[emerg]module"/usr/local/openresty/nginx/modules/ngx_http_ts_module.so"isnotbinarycompatiblein/usr/local/openresty/nginx/conf/nginx.conf......
  • LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree
    Youaregiventherootofabinarysearchtreeandanarrayqueriesofsizenconsistingofpositiveintegers.Finda2Darrayanswerofsizenwhereanswer[i]=[mini,maxi]:miniisthelargestvalueinthetreethatissmallerthanorequaltoqueries[......
  • MySQL备份恢复数据--binary-mode is enabled and mysql is run in non-interactive...
    使用mysqldump;MySQL自带的逻辑备份工具。mysqldump[选项]数据库名[表名]>脚本名mysqldump[选项]--数据库名[选项表名]>脚本名mysqldump[选项]--all-databases[选项]>脚本名备份mysqldump-hlocalhost-uwordpress-pwordpress_20200104>c......