diff --git a/config.yml b/config.yml index 0926d0d3..713939d7 100644 --- a/config.yml +++ b/config.yml @@ -458,9 +458,21 @@ notebook: - tsearch: 三分 code_ext: hpp test_ext: cpp + - knapsack_01: 01 背包 + code_ext: hpp + test_ext: cpp + - knapsack_mixed: 混合背包 + code_ext: hpp + test_ext: cpp + - knapsack_mixed_huge: 混合背包(贪心) + code_ext: hpp + test_ext: cpp - lis: 最长上升子序列 code_ext: hpp test_ext: cpp + - lev_dis: Levenshtein 距离 + code_ext: hpp + test_ext: cpp - hoverline: 悬线法 code_ext: hpp test_ext: cpp @@ -482,13 +494,16 @@ notebook: - smawk: SMAWK 算法 code_ext: hpp test_ext: cpp - - astar: A* 算法 + - larsch: LARSCH 算法 code_ext: hpp test_ext: cpp - - simplex: 单纯形法 + - larsch_2d: LARSCH 算法(2D) code_ext: hpp test_ext: cpp - - knapsack_mixed: 混合背包 + - astar: A* 算法 + code_ext: hpp + test_ext: cpp + - simplex: 单纯形法 code_ext: hpp test_ext: cpp graph: diff --git a/notebook.bib b/notebook.bib index d2b802a6..55cc57d7 100644 --- a/notebook.bib +++ b/notebook.bib @@ -440,3 +440,22 @@ @article{karp1978characterization year = {1978}, publisher = {Elsevier} } + +@article{larmore1991line, + title = {On-line dynamic programming with applications to the prediction of RNA secondary structure}, + author = {Larmore, Lawrence L and Schieber, Baruch}, + journal = {Journal of Algorithms}, + volume = {12}, + number = {3}, + pages = {490--515}, + year = {1991}, + publisher = {Elsevier} +} + +@inproceedings{aggarwal1986geometric, + title = {Geometric applications of a matrix searching algorithm}, + author = {Aggarwal, Alok and Klawe, Maria and Moran, Shlomo and Shor, Peter and Wilber, Robert}, + booktitle = {Proceedings of the second annual symposium on Computational geometry}, + pages = {285--292}, + year = {1986} +} diff --git a/src/code/ds/mono_stack.hpp b/src/code/ds/mono_stack.hpp index a9309a6b..94cf910e 100644 --- a/src/code/ds/mono_stack.hpp +++ b/src/code/ds/mono_stack.hpp @@ -10,28 +10,15 @@ class mono_stack { static CEXP Comp compare{}; vec s; - template - CEXP auto find_(cT_(T) x) NE { - if CEXP (use_bf) { - auto it = s.rbegin(); - while (it != s.rend() && compare(x, *it)) ++it; - return (--it).base(); - } else return upper_bound(s, x, compare); - } public: CEXPE mono_stack() NE = default; - template - CEXP void pop_greater(cT_(T) x) NE { s.erase(find_(x), s.end()); } - CEXP void push_nocheck(cT_(T) x) NE { s.push_back(x); } - template - CEXP void push(cT_(T) x) NE { pop_greater(x), s.push_back(x); } - template - CEXP void insert(cT_(T) x) NE { - if (auto it = find_(x); it == s.end()) s.push_back(x); - else *it = x; + CEXP void pop_greater(cT_(T) x) NE { + while (!s.empty() && compare(x, s.back())) s.pop_back(); } + CEXP void push_nocheck(cT_(T) x) NE { s.push_back(x); } + CEXP void push(cT_(T) x) NE { pop_greater(x), s.push_back(x); } CEXP T CR top() CNE { return s.back(); } CEXP u32 size() CNE { return (u32)s.size(); } }; diff --git a/src/code/opt/knapsack_01.hpp b/src/code/opt/knapsack_01.hpp new file mode 100644 index 00000000..0f2189ff --- /dev/null +++ b/src/code/opt/knapsack_01.hpp @@ -0,0 +1,59 @@ +#ifndef TIFALIBS_OPT_KNAPSACK_01 +#define TIFALIBS_OPT_KNAPSACK_01 + +#include "../util/traits.hpp" + +namespace tifa_libs::opt { + +//@param a a[i]={value, weight} +//@param W max weight +//@return max total value while total weight <= W +template +CEXP T knapsack_01(vecpt CR a, T W) NE { + const T vs = [k = std::views::keys(a)] { return std::reduce(k.begin(), k.end(), T{}); }(); + const T _1 = W, _2 = vs, + _3 = T(a.size() >= 128 ? -1_u64 : 1_u64 << ((a.size() + 1) / 2)), + _min = min({_1, _2, _3}); + assert(0_isz <= (isz)_min && (usz)_min * a.size() < (usz)1e9); + if (_min == _1) { + vec dp(W + 1); + for (auto [s, t] : a) + for (u32 j = u32(W - t); (i32)j >= 0; --j) dp[j + t] = max(dp[j + t], dp[j] + s); + return max(dp); + } else if (const T inf = to_uint_t(-1) / 2 - 1; _min == _2) { + vec dp(vs + 1, inf); + dp[0] = 0; + for (auto [s, t] : a) + for (u32 j = u32(vs - s); (i32)j >= 0; --j) dp[j + s] = min(dp[j + s], dp[j] + t); + T ans = 0; + flt_ (T, i, 0, T(vs + 1)) + if (dp[i] <= W) ans = max(ans, i); + return ans; + } else { + auto f = [&](u32 l, u32 r) { + vecpt res(1 << (r - l)); + res[0] = {0, 0}; + flt_ (u32, i, 0, r - l) + flt_ (u32, j, 0, 1 << i) { + auto&& [v, w] = a[l + i]; + res[je + j] = {res[j].first + w, res[j].second + v}; + } + sort(res); + flt_ (u32, i, 1, (u32)res.size()) res[i].second = max(res[i].second, res[i - 1].second); + return res; + }; + auto l = f(0, (u32)a.size() / 2), r = f((u32)a.size() / 2, (u32)a.size()); + reverse(l), r.emplace_back(inf, inf); + T ans = 0; + u32 id = -1_u32; + for (auto& [t, s] : l) { + while (t + r[id + 1].first <= W) ++id; + if (~id) ans = max(ans, s + r[id].second); + } + return ans; + } +} + +} // namespace tifa_libs::opt + +#endif \ No newline at end of file diff --git a/src/code/opt/knapsack_mixed_huge.hpp b/src/code/opt/knapsack_mixed_huge.hpp new file mode 100644 index 00000000..25f0cf62 --- /dev/null +++ b/src/code/opt/knapsack_mixed_huge.hpp @@ -0,0 +1,42 @@ +#ifndef TIFALIBS_OPT_KNAPSACK_MIXED_HUGE +#define TIFALIBS_OPT_KNAPSACK_MIXED_HUGE + +#include "../util/traits.hpp" + +namespace tifa_libs::opt { + +//@param a a[i]={value, weight, count} +//@param W max weight +//@return max total value while total weight <= W +template +CEXP T knapsack_mixed_huge(vec> a, T W) NE { + const u32 n = (u32)a.size(); + assert(n < 500); + const u32 lim = [k = max(n, 50_u32)] { return k * k * k + 1; }(); + vec dp(lim + 1, to_uint_t(-1) / 2 - 1); + dp[0] = 0; + for (auto& [v, w, num] : a) { + T have = min(num, (T)n); + num -= have; + for (T k = 1; k = min(have, k), have; have -= k, k <<= 1) + for (u32 j = lim - 1; (T)j >= v * k; --j) dp[j] = min(dp[j], dp[j - v * k] + w * k); + } + vecu id(n); + std::iota(id.begin(), id.end(), 0), sort(id, [&](auto x, auto y) { return a[x]._0 * a[y]._1 > a[y]._0 * a[x]._1; }); + T ans = 0; + flt_ (u32, j, 0, lim + 1) { + if (W < dp[j]) continue; + T rest = W - dp[j], now = j; + for (auto i : id) { + auto [v, w, num] = a[i]; + T div = min(num, rest / w); + rest -= div * w, now += div * v; + } + ans = max(ans, now); + } + return ans; +} + +} // namespace tifa_libs::opt + +#endif \ No newline at end of file diff --git a/src/code/opt/larsch.hpp b/src/code/opt/larsch.hpp new file mode 100644 index 00000000..9812a296 --- /dev/null +++ b/src/code/opt/larsch.hpp @@ -0,0 +1,31 @@ +#ifndef TIFALIBS_OPT_LARSCH +#define TIFALIBS_OPT_LARSCH + +#include "../util/traits.hpp" + +namespace tifa_libs::opt { + +template +requires requires(Func f, u32 x, u32 y) { {f(x,y)}->std::same_as; } +CEXP vec larsch(u32 n, Func&& w, T inf = inf_v) NE { + vec dp(n + 1, inf); + vecu x(n + 1); + auto chk = [&](u32 from, u32 to) { + if (from >= to) return; + if (T cost = w(from, to); dp[from] + cost < dp[to]) dp[to] = dp[from] + cost, x[to] = from; + }; + auto f = [&](auto&& f, u32 l, u32 r) -> void { + if (l + 1 >= r) return; + u32 m = l + (r - l) / 2; + for (u32 i = x[l]; i <= x[r]; i++) chk(i, m); + f(f, l, m); + for (u32 i = l + 1; i <= m; i++) chk(i, r); + f(f, m, r); + }; + dp[0] = 0, chk(0, n), f(f, 0, n); + return dp; +}; + +} // namespace tifa_libs::opt + +#endif \ No newline at end of file diff --git a/src/code/opt/larsch_2d.hpp b/src/code/opt/larsch_2d.hpp new file mode 100644 index 00000000..36f72a0a --- /dev/null +++ b/src/code/opt/larsch_2d.hpp @@ -0,0 +1,27 @@ +#ifndef TIFALIBS_OPT_LARSCH_2D +#define TIFALIBS_OPT_LARSCH_2D + +#include "../util/traits.hpp" +#include "smawk.hpp" + +namespace tifa_libs::opt { + +template +requires requires(Func f, u32 x, u32 y) { {f(x,y)}->std::same_as; } +CEXP vec larsch_2d(u32 n, Func&& w, T inf = inf_v) NE { + using W = to_bigger_t; + vec dp(n + 1, inf_v); + vec ans(n + 1, inf); + dp[0] = 0; + auto f = [&](u32 j, u32 i) -> W { return i < j ? dp[i] + w(i, j) : inf_v; }; + flt_ (u32, d, 1, n) { + auto argmin = smawk(n + 1, n + 1, [&](u32 i, u32 j, u32 k) { return f(i, j) <= f(i, k); }); + for (u32 i = n; i >= d; --i) dp[i] = dp[argmin[i]] + w(argmin[i], i); + ans[d] = (T)dp[n]; + } + return ans; +}; + +} // namespace tifa_libs::opt + +#endif \ No newline at end of file diff --git a/src/code/opt/lev_dis.hpp b/src/code/opt/lev_dis.hpp new file mode 100644 index 00000000..3f48a6ee --- /dev/null +++ b/src/code/opt/lev_dis.hpp @@ -0,0 +1,22 @@ +#ifndef TIFALIBS_OPT_LEV_DIS +#define TIFALIBS_OPT_LEV_DIS + +#include "../util/util.hpp" + +namespace tifa_libs::opt { + +//@return ans[i][j] = lev(a[0..i), b[0..j)) +template +CEXP vvecu lev_dis(T CR a, T CR b) NE { + const u32 n = (u32)a.size(), m = (u32)b.size(); + vvecu ans(n + 1, vecu(m + 1)); + flt_ (u32, i, 0, n + 1) ans[i][0] = i; + flt_ (u32, i, 0, m + 1) ans[0][i] = i; + flt_ (u32, i, 1, n + 1) + flt_ (u32, j, 1, m + 1) ans[i][j] = min({ans[i - 1][j - 1] + !(a[i - 1] == b[j - 1]), ans[i][j - 1] + 1, ans[i - 1][j] + 1}); + return ans; +} + +} // namespace tifa_libs::opt + +#endif \ No newline at end of file diff --git a/src/doc_md/opt/knapsack_01.md b/src/doc_md/opt/knapsack_01.md new file mode 100644 index 00000000..59765627 --- /dev/null +++ b/src/doc_md/opt/knapsack_01.md @@ -0,0 +1,4 @@ +--- +title: knapsack_01 +documentation_of: //src/code/opt/knapsack_01.hpp +--- diff --git a/src/doc_md/opt/knapsack_mixed_huge.md b/src/doc_md/opt/knapsack_mixed_huge.md new file mode 100644 index 00000000..42d608b7 --- /dev/null +++ b/src/doc_md/opt/knapsack_mixed_huge.md @@ -0,0 +1,4 @@ +--- +title: knapsack_mixed_huge +documentation_of: //src/code/opt/knapsack_mixed_huge.hpp +--- diff --git a/src/doc_md/opt/larsch.md b/src/doc_md/opt/larsch.md new file mode 100644 index 00000000..e5a71813 --- /dev/null +++ b/src/doc_md/opt/larsch.md @@ -0,0 +1,4 @@ +--- +title: larsch +documentation_of: //src/code/opt/larsch.hpp +--- diff --git a/src/doc_md/opt/larsch_2d.md b/src/doc_md/opt/larsch_2d.md new file mode 100644 index 00000000..c65c8f12 --- /dev/null +++ b/src/doc_md/opt/larsch_2d.md @@ -0,0 +1,4 @@ +--- +title: larsch_2d +documentation_of: //src/code/opt/larsch_2d.hpp +--- diff --git a/src/doc_md/opt/lev_dis.md b/src/doc_md/opt/lev_dis.md new file mode 100644 index 00000000..420be501 --- /dev/null +++ b/src/doc_md/opt/lev_dis.md @@ -0,0 +1,4 @@ +--- +title: lev_dis +documentation_of: //src/code/opt/lev_dis.hpp +--- diff --git a/src/doc_tex/opt/alpha_beta.tex b/src/doc_tex/opt/alpha_beta.tex index c39e8be4..d1b10917 100644 --- a/src/doc_tex/opt/alpha_beta.tex +++ b/src/doc_tex/opt/alpha_beta.tex @@ -8,6 +8,4 @@ 最好 \(O\left(\sqrt{b^d}\right)\), 最坏 \(O\left(b^d\right)\), 其中 \(b\) 为结点的最大出度, \(d\) 为树的深度. -\paragraph{参考文献} - -\cite{enwiki:1188156145} +\paragraph{参考文献} \cite{enwiki:1188156145} diff --git a/src/doc_tex/opt/knapsack_01.tex b/src/doc_tex/opt/knapsack_01.tex new file mode 100644 index 00000000..fa1c3753 --- /dev/null +++ b/src/doc_tex/opt/knapsack_01.tex @@ -0,0 +1 @@ +\paragraph{复杂度} \(O\left(N\min\left\{W , \sum_i v_i , 2^{\frac{N}{2}}\right\}\right)\) \ No newline at end of file diff --git a/src/doc_tex/opt/knapsack_mixed_huge.tex b/src/doc_tex/opt/knapsack_mixed_huge.tex new file mode 100644 index 00000000..9636750e --- /dev/null +++ b/src/doc_tex/opt/knapsack_mixed_huge.tex @@ -0,0 +1 @@ +适用于物品个数和价值均较小 (\(\leq 50\)) 且总容量较大 (\(10^9\)) 的情况 \ No newline at end of file diff --git a/src/doc_tex/opt/larsch.tex b/src/doc_tex/opt/larsch.tex new file mode 100644 index 00000000..58185e1d --- /dev/null +++ b/src/doc_tex/opt/larsch.tex @@ -0,0 +1,13 @@ +\fullref{sec:smawk-算法} 的变体, 大概适用于 1D1D 四边形不等式 + +\paragraph{输入} Monge 函数 \(w(x,y)\), 范围 \([0,n]\times[0,n]\) + +\paragraph{输出} \(a\), 其中 \(a_i=\texttt{dp}(i)\), \(\texttt{dp}(0)=0\), \(\texttt{dp}(i)=\min_{0\leq j0)\) + +易知 \(A\) 是 Monge 矩阵 + +\paragraph{复杂度} \(O(n\log n)\) + +\paragraph{参考链接} \qrcode{https://www.itcsc.cuhk.edu.hk/Winter_School/Winter_School_2010/Title_Abstract/PPT_Mordecai.pdf} \qrcode{https://www.cse.ust.hk/faculty/golin/Talks/Revisiting_Monge.pdf} \qrcode{https://noshi91.hatenablog.com/entry/2023/02/18/005856} + +\paragraph{参考文献} \cite{larmore1991line} \ No newline at end of file diff --git a/src/doc_tex/opt/larsch_2d.tex b/src/doc_tex/opt/larsch_2d.tex new file mode 100644 index 00000000..74179ab8 --- /dev/null +++ b/src/doc_tex/opt/larsch_2d.tex @@ -0,0 +1,9 @@ +大概适用于 2D1D 四边形不等式 + +\paragraph{输入} Monge 函数 \(w(x,y)\), 范围 \([0,n]\times[0,n]\) + +\paragraph{输出} \(a\), 其中 \(a_d=\texttt{dp}(n,d)\), \(\texttt{dp}(0,d)=0\), \(\texttt{dp}(i,d)=\min_{0\leq j0)\) + +\paragraph{参考链接} \qrcode{https://www.itcsc.cuhk.edu.hk/Winter_School/Winter_School_2010/Title_Abstract/PPT_Mordecai.pdf} \qrcode{https://www.cse.ust.hk/faculty/golin/Talks/Revisiting_Monge.pdf} + +\paragraph{参考文献} \cite{larmore1991line} \ No newline at end of file diff --git a/src/doc_tex/opt/lev_dis.tex b/src/doc_tex/opt/lev_dis.tex new file mode 100644 index 00000000..160300a7 --- /dev/null +++ b/src/doc_tex/opt/lev_dis.tex @@ -0,0 +1,9 @@ +对两个序列 \(a\), \(b\), 其 Levenshtein 距离 \(\operatorname{lev}(a,b)\) 定义为通过对 \(a\) 进行 (插入, 删除, 修改) 单个元素使其变为 \(b\) 的最少操作次数. 令 \(a'\) 表示 \(a\) 删除第一个元素后的序列, 则 + +\[ + \operatorname{lev}(a,b)=\begin{cases} + \max\{|a|,|b|\}, & |a|=0 ~\text{or}~ |b|=0, \\ + \operatorname{lev}(a',b'), & a_0=b_0, \\ + 1+\min\{\operatorname{lev}(a',b),\operatorname{lev}(a,b'),\operatorname{lev}(a',b')\}, & \text{otherwise}. + \end{cases} +\] \ No newline at end of file diff --git a/src/doc_tex/opt/smawk.tex b/src/doc_tex/opt/smawk.tex index 55f819f9..25cd046e 100644 --- a/src/doc_tex/opt/smawk.tex +++ b/src/doc_tex/opt/smawk.tex @@ -1,13 +1,19 @@ -用于计算满足如下条件的矩阵 \(a\) 每行的 \(\operatorname{argmin}\): +用于计算满足如下条件 (Monotone) 的矩阵 \(A\) 每行的 \(\operatorname{argmin}\): \begin{center} - 令 \(h(r) = \operatorname{argmin}_i a_{r,i}\), 则 \(h\) 单调不降 + 令 \(h(r) = \max \operatorname{argmin}_i A_{r,i}\), 则 \(h\) 单调不降 \end{center} 不难发现可以分治: 对当前矩阵, 找到中间一行的 \(\operatorname{argmin}\), 然后就可根据这个将矩阵分为更小的两个矩阵 +如果 Monotone 矩阵 \(A\) 还满足任意 \(2\times 2\) 的子矩阵都是 Monotone 的, 则称 \(A\) 是 Totally monotone 的 + +如果 Monotone 矩阵 \(A\) 还满足 \(\forall i,j,~A_{i,j}+A_{i+1,j+1}\leq A_{i+1,j}+A_{i,j+1}\), 则称 \(A\) 是 Monge 的 + +显然若 \(A\) 是 Monge 的则一定是 Totally monotone 的 + \paragraph{复杂度} \(O(c(1+\log(r/c)))\), 其中 \(r\) 为矩阵行数, \(c\) 为矩阵列数 -\paragraph{参考文献} +\paragraph{参考链接} \qrcode{https://www.itcsc.cuhk.edu.hk/Winter_School/Winter_School_2010/Title_Abstract/PPT_Mordecai.pdf} \qrcode{https://www.cse.ust.hk/faculty/golin/Talks/Revisiting_Monge.pdf} -\cite{enwiki:1033199110} +\paragraph{参考文献} \cite{aggarwal1986geometric} \cite{enwiki:1033199110} diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_b.knapsack_01.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_b.knapsack_01.test.cpp new file mode 100644 index 00000000..f30a786e --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_b.knapsack_01.test.cpp @@ -0,0 +1,13 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_B" + +#include "../../code/opt/knapsack_01.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u32 n, w; + std::cin >> n >> w; + vecpt a(n); + for (auto& [v, w] : a) std::cin >> v >> w; + std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; + return 0; +} diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_b.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_b.knapsack_mixed.test.cpp similarity index 100% rename from src/test_cpverifier/aizu-dpl/dpl_1_b.test.cpp rename to src/test_cpverifier/aizu-dpl/dpl_1_b.knapsack_mixed.test.cpp diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_e.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_e.test.cpp new file mode 100644 index 00000000..e8be661c --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_e.test.cpp @@ -0,0 +1,11 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_E" + +#include "../../code/opt/lev_dis.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + strn a, b; + std::cin >> a >> b; + std::cout << tifa_libs::opt::lev_dis(a, b).back().back() << '\n'; + return 0; +} diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_f.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_f.test.cpp new file mode 100644 index 00000000..3b90e444 --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_f.test.cpp @@ -0,0 +1,13 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_F" + +#include "../../code/opt/knapsack_01.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u32 n, w; + std::cin >> n >> w; + vecpt a(n); + for (auto& [v, w] : a) std::cin >> v >> w; + std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; + return 0; +} diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_g.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_g.test.cpp new file mode 100644 index 00000000..0804b3f9 --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_g.test.cpp @@ -0,0 +1,18 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_G" + +#include "../../code/opt/knapsack_mixed.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u32 n, w; + std::cin >> n >> w; + tifa_libs::opt::knapsack_mixed kn(w); + for (u32 i = 0; i < n; ++i) { + i32 v; + u32 w, m; + std::cin >> v >> w >> m; + kn.add(w, v, m); + } + std::cout << *std::ranges::max_element(kn.result()) << '\n'; + return 0; +} \ No newline at end of file diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_h.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_h.test.cpp new file mode 100644 index 00000000..db08bc6a --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_h.test.cpp @@ -0,0 +1,13 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_H" + +#include "../../code/opt/knapsack_01.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u64 n, w; + std::cin >> n >> w; + vecpt a(n); + for (auto& [v, w] : a) std::cin >> v >> w; + std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; + return 0; +} diff --git a/src/test_cpverifier/aizu-dpl/dpl_1_i.test.cpp b/src/test_cpverifier/aizu-dpl/dpl_1_i.test.cpp new file mode 100644 index 00000000..3a624c3d --- /dev/null +++ b/src/test_cpverifier/aizu-dpl/dpl_1_i.test.cpp @@ -0,0 +1,13 @@ +#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_I" + +#include "../../code/opt/knapsack_mixed_huge.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u64 n, w; + std::cin >> n >> w; + vec a(n); + for (auto& [v, w, num] : a) std::cin >> v >> w >> num; + std::cout << tifa_libs::opt::knapsack_mixed_huge(a, w) << '\n'; + return 0; +} diff --git a/src/test_cpverifier/aizu/3086.test.cpp b/src/test_cpverifier/aizu/3086.test.cpp new file mode 100644 index 00000000..b3bc4e5b --- /dev/null +++ b/src/test_cpverifier/aizu/3086.test.cpp @@ -0,0 +1,20 @@ +#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3086" + +#include "../../code/ds/segtree.hpp" +#include "../../code/opt/larsch.hpp" + +i64 op(i64 x, i64 y) { return std::min(x, y); } +void mapping(i64& x, i64 y) { x = op(x, y); } + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u32 n, L; + std::cin >> n >> L; + vecii a(n); + for (auto& i : a) std::cin >> i, i = -i; + const auto inf = tifa_libs::inf_v; + tifa_libs::ds::segtree_notag tr(inf, a); + auto ans = tifa_libs::opt::larsch(n, [&](u32 l, u32 r) { return r < l + L ? inf : tr.query(l, r); }); + std::cout << -ans.back() << '\n'; + return 0; +} diff --git a/src/test_cpverifier/yukicoder/0952.test.cpp b/src/test_cpverifier/yukicoder/0952.test.cpp new file mode 100644 index 00000000..88147da4 --- /dev/null +++ b/src/test_cpverifier/yukicoder/0952.test.cpp @@ -0,0 +1,16 @@ +#define PROBLEM "https://yukicoder.me/problems/no/952" + +#include "../../code/opt/larsch_2d.hpp" + +int main() { + std::cin.tie(nullptr)->std::ios::sync_with_stdio(false); + u32 n; + std::cin >> n; + vecii a(n), b(n + 1); + for (auto& i : a) std::cin >> i; + std::inclusive_scan(a.begin(), a.end(), b.begin() + 1); + auto w = [&](u32 i, u32 j) -> i64 { return i + 1 == j ? 0 : (b[j - 1] - b[i]) * (b[j - 1] - b[i]); }; + auto ans = tifa_libs::opt::larsch_2d(n + 1, w); + for (u32 i = n; i; --i) std::cout << ans[i] << '\n'; + return 0; +} diff --git a/src/test_tinplate/opt/knapsack_01.cpp b/src/test_tinplate/opt/knapsack_01.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/test_tinplate/opt/knapsack_mixed_huge.cpp b/src/test_tinplate/opt/knapsack_mixed_huge.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/test_tinplate/opt/larsch.cpp b/src/test_tinplate/opt/larsch.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/test_tinplate/opt/larsch_2d.cpp b/src/test_tinplate/opt/larsch_2d.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/test_tinplate/opt/lev_dis.cpp b/src/test_tinplate/opt/lev_dis.cpp new file mode 100644 index 00000000..e69de29b