Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add monotone_minima, online_offline_dp #113

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"test/yosupo-lca.2.test.cpp": "2024-05-17 22:45:13 +0900",
"test/yosupo-line-add-get-min.test.cpp": "2023-06-19 07:11:52 +0900",
"test/yosupo-log-of-fps.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-min-plus-convolution-convex-and-arbitary.test.cpp": "2024-04-24 16:21:33 +0900",
"test/yosupo-min-plus-convolution-convex-and-convex.test.cpp": "2024-04-24 16:21:33 +0900",
"test/yosupo-partition-function_1.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-partition-function_2.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-point-add-rectangle-sum.test.cpp": "2024-05-17 23:06:28 +0900",
Expand Down Expand Up @@ -71,6 +73,7 @@
"test/yukicoder-186.test.cpp": "2023-09-16 00:07:15 +0900",
"test/yukicoder-187.test.cpp": "2023-09-16 00:07:15 +0900",
"test/yukicoder-2292.test.cpp": "2024-05-17 23:11:56 +0900",
"test/yukicoder-705.test.cpp": "2024-04-24 16:21:33 +0900",
"test/yukicoder-rotate-enlarge_1.test.cpp": "2023-10-12 08:50:40 +0900",
"test/yukicoder-rotate-enlarge_2.test.cpp": "2023-10-12 08:50:40 +0900",
"test/yukicoder-search-oji.test.cpp": "2023-10-12 08:50:40 +0900"
Expand Down
121 changes: 121 additions & 0 deletions cpp/convex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#pragma once
#include <cassert>
#include <functional>
#include <limits>
#include <stack>
#include <type_traits>
#include <optional>
#include <vector>

/**
* @brief monotoneな行列において、各行の最小値と、それを取る最小列番号を$O((H+W)\log H)$時間で得る
* @param h 行数(行番号は[0..h-1])
* @param w 列数(列番号は[0..w-1])
* @param f 行列の要素を与える関数(行番号と列番号を引数にとって値を返す)
* @param comp 要素の比較関数(最小値を知りたい場合はデフォルトのstd::less<T>)
* @return std::pair<std::vector<T>, std::vector<int>> (各行の最小値, 各行の最小値列番号)
*/
template <typename F, typename Comp = std::less<std::invoke_result_t<F, int, int>>>
std::pair<std::vector<std::invoke_result_t<F, int, int>>, std::vector<int>> monotone_minima(int h, int w, const F& f, const Comp& comp = Comp()) {
using T = std::invoke_result_t<F, int, int>;
assert(h >= 0);
assert(w >= 0);
std::vector<T> res_value(h);
std::vector<int> res_idx(h);
std::stack<std::tuple<int, int, int, int>> stk; // {i, j, l, r} : [i..j]行目の答えを求める。結果は[l..r]の範囲に収まることが保証されている。
stk.emplace(0, h-1, 0, w-1);
while(!stk.empty()) {
auto [i, j, l, r] = stk.top(); stk.pop();
int m = (i+j) / 2;
T min_value = f(m, l);
int min_idx = l;
for(int k = l+1; k <= r; k++) {
T value = f(m, k);
if(comp(value, min_value)) {
min_value = value;
min_idx = k;
}
}
res_value[m] = min_value;
res_idx[m] = min_idx;
if(i <= m-1) stk.emplace(i, m-1, l, min_idx);
if(m+1 <= j) stk.emplace(m+1, j, min_idx, r);
}
return {res_value, res_idx};
}

/**
* @brief オンライン・オフライン変換による、DAGで辺のコストがmonotoneな場合の最短経路問題を$O(N \log^2 N)$時間で求める
* @param n ノード数-1 (ノード番号は[0..n])
* @param f 辺のコスト(i<jのときf(i, j)が辺のコストを返す)
* @param comp 比較関数
* @return 頂点0から頂点[0..n]へのコストを表す長さn+1のvector
*/
template <typename F, typename Comp = std::less<std::invoke_result_t<F, int, int>>>
std::vector<std::invoke_result_t<F, int, int>> online_offline_dp(int n, const F& f, const Comp& comp = Comp{}) {
using T = std::invoke_result_t<F, int, int>;
std::vector<std::optional<T>> dp(n+1, std::nullopt);
dp[0] = 0;
// dp[l..r)の要素を、dp[l..r)からの遷移のみの範囲で求める
auto solve_subproblem = [&](auto self, int l, int r) -> void {
int mid = (l+r) / 2;
if(mid-l >= 2) self(self, l, mid);
auto submat = [&](int i, int j) {
return dp[l+j].value() + f(l+j, mid+i);
};
std::vector<T> min_val = monotone_minima(r-mid, mid-l, submat, comp).first;
for(int i = 0; i < r-mid; i++) {
if(!dp[mid+i] || comp(min_val[i], dp[mid+i].value())) {
dp[mid+i] = min_val[i];
}
}
if(r-mid >= 2) self(self, mid, r);
};
solve_subproblem(solve_subproblem, 0, n+1);
std::vector<T> res(n+1);
for(int i = 0; i <= n; i++) res[i] = dp[i].value();
return res;
}

/**
* @brief aがconvex (a_{i+1}-a_i <= a_{i+2}-a_{i+1})のとき、c_k = min_{i+j=k}(a_i + b_j)なる長さN+M-1の配列を得る O((N+m)\log(N+M))
* @param a vector 配列1 (convexである必要があります)
* @param b vector 配列2
* @return c vector min-plus convolutionの結果
*/
template <typename T, std::enable_if_t<std::is_scalar_v<T>, std::nullptr_t> = nullptr>
std::vector<T> min_plus_convolution_convex_arbitary(const std::vector<T>& a, const std::vector<T>& b) {
int n = a.size(), m = b.size();
for(int i = 0; i < n-2; i++) assert(a[i+1]-a[i] <= a[i+2]-a[i+1]);
auto mat = [&](int i, int j) {
int k = i-j;
if(k < 0) return std::numeric_limits<T>::max();
if(k >= n) return std::numeric_limits<T>::max();
return a[k] + b[j];
};
return monotone_minima(n+m-1, m, mat).first;
}

/**
* @brief aもbもconvex (a_{i+1}-a_i <= a_{i+2}-a_{i+1})のとき、c_k = min_{i+j=k}(a_i + b_j)なる長さN+M-1の配列を得る O(N+M)
* @param a vector 配列1 (convexである必要があります)
* @param b vector 配列2
* @return c vector min-plus convolutionの結果
*/
template <typename T, std::enable_if_t<std::is_scalar_v<T>, std::nullptr_t> = nullptr>
std::vector<T> min_plus_convolution_convex_convex(const std::vector<T>& a, const std::vector<T>& b) {
int n = a.size(), m = b.size();
for(int i = 0; i < n-2; i++) assert(a[i+1]-a[i] <= a[i+2]-a[i+1]);
for(int i = 0; i < m-2; i++) assert(b[i+1]-b[i] <= b[i+2]-b[i+1]);
int i = 0, j = 0;
std::vector<T> c(n+m-1);
c[0] = a[i] + b[j];
for(int k = 1; k < n+m-1; k++) {
if(j == m-1 || (i < n-1 && a[i+1]+b[j] < a[i]+b[j+1])) {
c[k] = a[++i] + b[j];
} else {
c[k] = a[i] + b[++j];
}
}
return c;
}
14 changes: 14 additions & 0 deletions test/yosupo-min-plus-convolution-convex-and-arbitary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define PROBLEM "https://judge.yosupo.jp/problem/min_plus_convolution_convex_arbitrary"
#include "../cpp/convex.hpp"
#include <iostream>

int main() {
int n, m; std::cin >> n >> m;
std::vector<int> a(n), b(m);
for(int i = 0; i < n; i++) std::cin >> a[i];
for(int i = 0; i < m; i++) std::cin >> b[i];
std::vector<int> c = min_plus_convolution_convex_arbitary(a, b);
for(int i = 0; i < n+m-1; i++) {
std::cout << c[i] << (i==n+m-2 ? '\n' : ' ');
}
}
14 changes: 14 additions & 0 deletions test/yosupo-min-plus-convolution-convex-and-convex.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define PROBLEM "https://judge.yosupo.jp/problem/min_plus_convolution_convex_convex"
#include "../cpp/convex.hpp"
#include <iostream>

int main() {
int n, m; std::cin >> n >> m;
std::vector<int> a(n), b(m);
for(int i = 0; i < n; i++) std::cin >> a[i];
for(int i = 0; i < m; i++) std::cin >> b[i];
std::vector<int> c = min_plus_convolution_convex_convex(a, b);
for(int i = 0; i < n+m-1; i++) {
std::cout << c[i] << (i==n+m-2 ? '\n' : ' ');
}
}
18 changes: 18 additions & 0 deletions test/yukicoder-705.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#define PROBLEM "https://yukicoder.me/problems/no/705"
#include "../cpp/convex.hpp"
#include <iostream>

int main() {
using ll = long long;
int n; std::cin >> n;
std::vector<ll> a(n), x(n), y(n);
for(int i = 0; i < n; i++) std::cin >> a[i];
for(int i = 0; i < n; i++) std::cin >> x[i];
for(int i = 0; i < n; i++) std::cin >> y[i];
auto f = [&](int i, int j) {
ll dx = std::abs(x[i] - a[j-1]);
ll dy = std::abs(y[i]);
return dx*dx*dx + dy*dy*dy;
};
std::cout << online_offline_dp(n, f)[n] << std::endl;
}
Loading