monotone minima (algorithm/monotone_minima.cpp)
- category: algorithm
-
View this file on GitHub
- Last commit date: 2020-04-06 04:52:14+09:00
Depends on
Required by
Verified with
- test/aoj_2580.test.cpp
- test/yc_703_onoff.test.cpp
- test/yc_704_onoff.test.cpp
- test/yc_705_onoff.test.cpp
Code
Bundle
Copy
#ifndef H_monotone_minima
#define H_monotone_minima
/**
* @brief monotone minima
* @author えびちゃん
*/
#include <cstddef>
#include <utility>
#include <vector>
#include "utility/make/fix_point.cpp"
template <typename Fn>
auto monotone_minima(Fn&& f, size_t h, size_t w) {
using value_type = decltype(f(h, w));
std::vector<size_t> res(h);
make_fix_point([&](auto dfs, size_t hl, size_t hu, size_t wl, size_t wu) -> void {
if (hl >= hu) return;
size_t hm = (hl+hu) >> 1;
value_type min = f(hm, wl);
res[hm] = wl;
for (size_t j = wl+1; j < wu; ++j) {
value_type cur = f(hm, j);
if (cur < min) {
min = std::move(cur);
res[hm] = j;
}
}
if (hl == hm) return;
dfs(hl, hm, wl, res[hm]+1);
dfs(hm+1, hu, res[hm], wu);
})(0, h, 0, w);
return res;
}
#endif /* !defined(H_monotone_minima) */