diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f977d42..5ebe29d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,159 @@ # Changelog for Jikka +## 2021-07-21: v5.0.11.0 + +Convex Hull Trick and Segment Trees are implemented. + +Input (`examples/dp_z.py`): + +```python +# https://atcoder.jp/contests/dp/tasks/dp_z +from typing import * + +INF = 10 ** 18 + +def solve(n: int, c: int, h: List[int]) -> int: + assert 2 <= n <= 10 ** 5 + assert 1 <= c <= 10 ** 12 + assert len(h) == n + assert all(1 <= h_i <= 10 ** 6 for h_i in h) + + dp = [INF for _ in range(n)] + dp[0] = 0 + for i in range(1, n): + for j in range(i): + dp[i] = min(dp[i], dp[j] + (h[j] - h[i]) ** 2 + c) + return dp[n - 1] + +def main() -> None: + n, c = map(int, input().split()) + h = list(map(int, input().split())) + assert len(h) == n + ans = solve(n, c, h) + print(ans) + +if __name__ == '__main__': + main() +``` + +Output (): + +```c++ +#include "jikka/base.hpp" +#include "jikka/convex_hull_trick.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +int64_t solve(int64_t n_0, int64_t c_1, std::vector h_2) { + std::vector x3; + x3.push_back(0); + jikka::convex_hull_trick x4_10; + for (int32_t x5 = 0; x5 < n_0 - 1; ++x5) { + x4_10.add_line(h_2[x5], h_2[x5] * h_2[x5] + x3[x5]); + x3.push_back(c_1 + h_2[(x5 + 1)] * h_2[(x5 + 1)] + + x4_10.get_min(h_2[(x5 + 1)] * -2)); + } + return x3[(n_0 - 1)]; +} +int main() { + int64_t n_12 = -1; + int64_t c_13 = -1; + std::cin >> n_12; + std::vector h_14(n_12, -1); + std::cin >> c_13; + for (int32_t i15 = 0; i15 < n_12; ++i15) { + std::cin >> h_14[i15]; + } + auto ans_16 = solve(n_12, c_13, h_14); + std::cout << ans_16 << ' '; + std::cout << '\n' << ' '; +} +``` + +Input (`examples/dp_q.py`): + +```python +# https://atcoder.jp/contests/dp/tasks/dp_q +from typing import * + +def solve(n: int, h: List[int], a: List[int]) -> int: + assert 1 <= n <= 2 * 10 ** 5 + assert len(h) == n + assert all(1 <= h_i <= n for h_i in h) + assert len(a) == n + assert all(1 <= a_i <= 10 ** 9 for a_i in a) + + dp = [0 for _ in range(n)] + for i in range(n): + b = 0 + for j in range(h[i]): + b = max(b, dp[j]) + dp[h[i] - 1] = b + a[i] + return max(dp) + +def main() -> None: + n = int(input()) + h = list(map(int, input().split())) + assert len(h) == n + a = list(map(int, input().split())) + assert len(a) == n + ans = solve(n, h, a) + print(ans) + +if __name__ == '__main__': + main() +``` + +Output (): + +```c++ +#include "jikka/base.hpp" +#include "jikka/segment_tree.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +int64_t solve(int64_t n_0, std::vector h_1, std::vector a_2) { + std::vector x4(n_0, 0); + atcoder::segtree x5_13( + x4); + for (int32_t x6 = 0; x6 < n_0; ++x6) { + x4[(h_1[x6] - 1)] = std::max(0, x5_13.prod(0, h_1[x6])) + a_2[x6]; + x5_13.set(h_1[x6] - 1, x4[(h_1[x6] - 1)]); + } + int64_t x11 = *std::max_element(x4.begin(), x4.end()); + return x11; +} +int main() { + int64_t n_14 = -1; + std::cin >> n_14; + std::vector h_15(n_14, -1); + std::vector a_16(n_14, -1); + for (int32_t i17 = 0; i17 < n_14; ++i17) { + std::cin >> h_15[i17]; + } + for (int32_t i18 = 0; i18 < n_14; ++i18) { + std::cin >> a_16[i18]; + } + auto ans_19 = solve(n_14, h_15, a_16); + std::cout << ans_19 << ' '; + std::cout << '\n' << ' '; +} +``` + + ## 2021-07-21: v5.0.10.0 - The generated C++ code is optimized. diff --git a/examples/README.md b/examples/README.md index 8c5072b7..2dcb1f42 100644 --- a/examples/README.md +++ b/examples/README.md @@ -27,7 +27,7 @@ - :heavy_check_mark: AC `dp_z.py` - Educational DP Contest [Z - Frog 3](https://atcoder.jp/contests/dp/tasks/dp_z) - Convex hull trick - - submission at v5.0.11.0: + - submission at v5.0.11.0: - :hourglass: TLE `abc134_c.py` - AtCoder Beginner Contest 134 [C - Exception Handling](https://atcoder.jp/contests/abc134/tasks/abc134_c) - Cumulative sums from both sides / 両側からの累積和 diff --git a/package.yaml b/package.yaml index 2a11c13c..7ae4c59e 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: Jikka -version: 5.0.10.0 +version: 5.0.11.0 github: "kmyk/Jikka" license: Apache author: "Kimiyuki Onaka"