generated from Tiphereth-A/TINplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2743d39
commit 57fcede
Showing
21 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#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 <int_c T> | ||
CEXP T knapsack_01(vecpt<T> 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}); | ||
if (_min == _1) { | ||
vec<T> dp(W + 1); | ||
for (auto [s, t] : a) | ||
for (u32 j = u32(W - t); ~j; --j) dp[j + t] = max(dp[j + t], dp[j] + s); | ||
return max(dp); | ||
} else if (const T inf = to_uint_t<T>(-1) / 2 - 1; _min == _2) { | ||
vec<T> dp(vs + 1, inf); | ||
dp[0] = 0; | ||
for (auto [s, t] : a) | ||
for (u32 j = u32(vs - s); ~j; --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<T> 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] : a) { | ||
while (t + r[id + 1].first <= W) ++id; | ||
if (~id) ans = max(ans, s + r[id].second); | ||
} | ||
return ans; | ||
} | ||
} | ||
|
||
} // namespace tifa_libs::opt | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <int_c T> | ||
CEXP T knapsack_mixed_huge(vec<pt3<T>> a, T W) NE { | ||
const u32 n = (u32)a.size(); | ||
assert(n < 500); | ||
const u32 lim = (n + 1) * (n + 1) * (n + 1) + 1; | ||
vec<T> dp(lim + 1, to_uint_t<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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <common_range T> | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: knapsack_01 | ||
documentation_of: //src/code/opt/knapsack_01.hpp | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: knapsack_mixed_huge | ||
documentation_of: //src/code/opt/knapsack_mixed_huge.hpp | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: lev_dis | ||
documentation_of: //src/code/opt/lev_dis.hpp | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
\paragraph{复杂度} \(O\left(N\min\left\{W , \sum_i v_i , 2^{\frac{N}{2}}\right\}\right)\) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
\] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<u32> a(n); | ||
for (auto& [v, w] : a) std::cin >> v >> w; | ||
std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; | ||
return 0; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<u32> a(n); | ||
for (auto& [v, w] : a) std::cin >> v >> w; | ||
std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<i32> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<u64> a(n); | ||
for (auto& [v, w] : a) std::cin >> v >> w; | ||
std::cout << tifa_libs::opt::knapsack_01(a, w) << '\n'; | ||
return 0; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test_cpverifier/aizu-dpl/dpl_1_h.knapsack_mixed_huge.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_1_H" | ||
|
||
#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<pt3uu> a(n); | ||
for (auto& [v, w, num] : a) std::cin >> v >> w, num = 1; | ||
std::cout << tifa_libs::opt::knapsack_mixed_huge(a, w) << '\n'; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<pt3uu> 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; | ||
} |
Empty file.
Empty file.
Empty file.