結果
問題 |
No.705 ゴミ拾い Hard
|
ユーザー |
👑 |
提出日時 | 2024-10-27 14:34:38 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 115 ms / 1,500 ms |
コード長 | 2,484 bytes |
コンパイル時間 | 3,162 ms |
コンパイル使用メモリ | 249,824 KB |
実行使用メモリ | 13,824 KB |
最終ジャッジ日時 | 2024-10-27 14:34:46 |
合計ジャッジ時間 | 7,468 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/no/705" #include <bits/stdc++.h> template <typename T = long long, typename Func> T monge_shortest_path(int n, Func cost_function) { assert(n > 0); std::vector<T> dist(n); std::vector<int> amin(n, 0); auto update = [&](int i, int k) { if (i <= k) return; T c = dist[k] + cost_function(k, i); if (c < dist[i]) { dist[i] = c; amin[i] = k; } }; auto rec_solve = [&](auto &&self, int l, int r) -> void { if (r - l == 1) return; int m = (l + r) / 2; for (int k = amin[l]; k <= amin[r]; k++) { update(m, k); } self(self, l, m); for (int k = l + 1; k <= m; k++) { update(r, k); } self(self, m, r); }; dist[0] = T(); for (int i = 1; i < n; i++) { dist[i] = cost_function(0, i); } rec_solve(rec_solve, 0, n - 1); return dist.back(); } void solve() { int n; std::cin >> n; std::vector<long long> A(n); std::vector<long long> X(n); std::vector<long long> Y(n); for (auto &a : A) std::cin >> a; for (auto &x : X) std::cin >> x; for (auto &y : Y) std::cin >> y; auto f = [&](int l, int r) { assert(l < r); long long dx = abs(A[r - 1] - X[l]); long long dy = Y[l]; return dx * dx * dx + dy * dy * dy; }; long long ans = monge_shortest_path(n + 1, f); std::cout << ans << std::endl; } int main() { std::cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while (t--) solve(); } // #define PROBLEM "https://yukicoder.me/problems/no/705" // // #include <bits/stdc++.h> // // #include "graph/monge_shortest_path.hpp" // // void solve() { // int n; // std::cin >> n; // std::vector<long long> A(n); // std::vector<long long> X(n); // std::vector<long long> Y(n); // for (auto &a : A) std::cin >> a; // for (auto &x : X) std::cin >> x; // for (auto &y : Y) std::cin >> y; // // auto f = [&](int l, int r) { // assert(l < r); // long long dx = abs(A[r - 1] - X[l]); // long long dy = Y[l]; // return dx * dx * dx + dy * dy * dy; // }; // // long long ans = monge_shortest_path(n + 1, f); // std::cout << ans << std::endl; // } // // int main() { // std::cin.tie(0)->sync_with_stdio(0); // // int t = 1; // // cin >> t; // while (t--) solve(); // }