Editorial for Bedao Regular Contest 18 - Thiết Kế Dự Án
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Ta sẽ chia lưới ô vuông ra làm các "lớp" như hình sau:
Trong đó, lớp thứ ~i~ bao gồm các điểm tham quan ở tọa độ ~(x,y)~ có ~max(x,y) = i~.
Ta nhận rằng ta cần thăm hết các điểm tham quan ở từng lớp một theo thứ tự từ bé tới lớn. Như vậy ta chỉ cần quan tâm tới hai điểm tham quan "trái dưới" và "phải trên" của từng lớp, vì khi đi ta sẽ đi từ một trong hai điểm này tới điểm còn lại để thăm hết lớp đó.
Vậy ta gọi ~dp(cnt,0/1)~ là đi tới lớp thứ ~cnt~, đang ở ô trái dưới hoặc phải trên (tùy vào ~0/1~) thì chi phí nhỏ nhất là bao nhiêu. Do có rất nhiều lớp nên ta chỉ quan tâm lớp nào xuất hiện, có thể dùng map<int,int>
để nén lại.
#include <bits/stdc++.h> using namespace std; using ll = long long; #define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u) #define db(val) "["#val" = "<<(val)<<"] " #define CONCAT_(x, y) x##y #define CONCAT(x, y) CONCAT_(x, y) #ifdef LOCAL # define clog cerr << setw(__db_level * 2) << setfill(' ') << "" << setw(0) # define DB() debug_block CONCAT(dbbl, __LINE__) int __db_level = 0; struct debug_block { debug_block() { clog << "{" << endl; ++__db_level; } ~debug_block() { --__db_level; clog << "}" << endl; } }; #else # define clog if (0) cerr # define DB(...) #endif template<class U, class V> print_op(pair<U, V>) { return out << "(" << u.first << ", " << u.second << ")"; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator<<(ostream& out, const Con& con) { out << "{"; for (auto beg = con.begin(), it = beg; it != con.end(); ++it) out << (it == beg ? "" : ", ") << *it; return out << "}"; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } const int N = 5e5 + 5; int n; map<int, vector<pair<int, int>>> mp; ll dp[N][2], cnt = 0; bool comp(pair<int, int>& u, pair<int, int>& v) { if (u.first == v.first) { return u.second > v.second; } return u.first < v.first; } int cost(pair<int, int>& u, pair<int, int>& v) { return abs(u.first - v.first) + abs(u.second - v.second); } void solve() { cin >> n; for (int i = 1; i <= n; ++ i) { int x, y; cin >> x >> y; mp[max(x, y)].push_back({x, y}); } memset(dp, 0x3f, sizeof dp); mp[0].push_back({0, 0}); int pre = 0; dp[0][0] = dp[0][1] = 0; for (auto& [u, vt] : mp) { sort(vt.begin(), vt.end(), comp); ++ cnt; dp[cnt][0] = min(dp[cnt - 1][0] + cost(mp[pre][0], vt.back()), dp[cnt - 1][1] + cost(mp[pre].back(), vt.back())) + cost(vt.back(), vt[0]); dp[cnt][1] = min(dp[cnt - 1][0] + cost(mp[pre][0], vt[0]), dp[cnt - 1][1] + cost(mp[pre].back(), vt[0])) + cost(vt.back(), vt[0]); pre = u; } cout << min(dp[cnt][0], dp[cnt][1]); } int main() { cin.tie(0)->sync_with_stdio(0); solve(); return 0; }
Comments