Submission #785123

#TimeUsernameProblemLanguageResultExecution timeMemory
785123eltu0815Sightseeing in Kyoto (JOI22_kyoto)C++14
0 / 100
1 ms340 KiB
#include <bits/stdc++.h> #define MAX 500005 #define MOD (ll)(1e9+7) #define INF (ll)(1e18) #define inf (1987654321) using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; int n, m, q; int a[100005], b[100005]; ll ans1[100005], ans2[100005]; int prea[100005], preb[100005]; int posta[100005], postb[100005]; deque<pii> graph1, graph2; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 1; i <= n; ++i) cin >> a[i]; for(int i = 1; i <= m; ++i) cin >> b[i]; prea[1] = 1, preb[1] = 1; for(int i = 2; i <= n; ++i) prea[i] = a[prea[i - 1]] >= a[i] ? i : prea[i - 1]; for(int i = 2; i <= m; ++i) preb[i] = b[preb[i - 1]] >= b[i] ? i : preb[i - 1]; posta[n] = n, postb[m] = m; for(int i = n - 1; i >= 1; --i) posta[i] = a[posta[i + 1]] >= a[i] ? i : posta[i + 1]; for(int i = m - 1; i >= 1; --i) postb[i] = b[postb[i + 1]] >= b[i] ? i : postb[i + 1]; auto solve1 = [&](auto&& self, int r, int c) -> void { if(r == 0 || c == 0) return; int i = prea[r], j = preb[c]; graph1.push_front({i, j}); self(self, i - 1, j - 1); }; auto solve2 = [&](auto&& self, int r, int c) -> void { if(r == n + 1 || c == m + 1) return; int i = posta[r], j = postb[c]; graph2.push_back({i, j}); self(self, i + 1, j + 1); }; solve1(solve1, n, m); graph1.push_front({1, 1}); for(int i = 1; i < (int)graph1.size(); ++i) { ans1[i] = INF; for(int j = 0; j < i; ++j) { ll r = graph1[i].first - graph1[j].first, c = graph1[i].second - graph1[j].second; ll cost = min(ans1[j] + 1ll * a[graph1[j].first] * c + 1ll * b[graph1[i].second] * r, ans1[j] + 1ll * b[graph1[j].second] * r + 1ll * a[graph1[i].first] * c); ans1[i] = min(ans1[i], cost); } } solve2(solve2, 1, 1); graph2.push_back({n, m}); for(int i = 1; i < (int)graph2.size(); ++i) { ans2[i] = INF; for(int j = 0; j < i; ++j) { ll r = graph2[i].first - graph2[j].first, c = graph2[i].second - graph2[j].second; ll cost = min(ans2[j] + 1ll * a[graph2[j].first] * c + 1ll * b[graph2[i].second] * r, ans2[j] + 1ll * b[graph2[j].second] * r + 1ll * a[graph2[i].first] * c); ans2[i] = min(ans2[i], cost); } } ll ans = ans1[graph1.size() - 1] + ans2[graph2.size() - 1]; ans += 1ll * a[graph1.back().first] * (graph2.front().second - graph1.back().second); ans += 1ll * b[graph2.front().second] * (graph2.front().first - graph1.back().first); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...