Submission #958323

#TimeUsernameProblemLanguageResultExecution timeMemory
958323riaritiPalembang Bridges (APIO15_bridge)C++17
31 / 100
2044 ms7888 KiB
#line 1 "Palembang_Bridges.cpp" #include <bits/stdc++.h> #ifdef local #define dbg(__VA_ARGS__) \ std::cerr << "[DBG|" << __LINE__ << "]: " << __VA_ARGS__ << std::endl; #define cerr(__VA_ARGS__) std::cerr << __VA_ARGS__; #else #define dbg(...) #define cerr(...) #endif void vec_out(const std::vector<int> &cc) { dbg("vector: "); for (auto x : cc) { cerr(x << " "); } cerr("\n"); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int K, N; std::cin >> K >> N; std::vector<std::tuple<char, std::int64_t, char, std::int64_t>> PSQT(N); for (auto &[P, S, Q, T] : PSQT) { std::cin >> P >> S >> Q >> T; } std::int64_t ans = 0; std::vector<std::pair<std::int64_t, std::int64_t>> HW; for (auto [P, S, Q, T] : PSQT) { if (P == Q) { ans += std::abs(T - S); continue; } HW.emplace_back(S, T); } auto calc = [&](int S, int T, int x) { return std::abs(S - x) + std::abs(T - x) + 1; }; if (K == 1) { auto f = [&](int x) { std::int64_t ans = 0; for (auto [S, T] : HW) { ans += calc(S, T, x); } return ans; }; std::int64_t l = 0, r = 1'000'000'001; while (l <= r) { auto m = l + (r - l) / 2; if (f(m) <= f(m + 1)) { r = m - 1; } else { l = m + 1; } } ans += f(l); std::cout << ans << "\n"; return 0; } std::set<int> st; for (auto [P, S, Q, T] : PSQT) { st.insert(S); st.insert(T); } std::int64_t tan = LLONG_MAX; for (auto i : st) { for (auto j : st) { if (i == j) { continue; } std::int64_t res = 0; for (auto [H, W] : HW) { res += std::min(calc(H, W, i), calc(H, W, j)); } tan = std::min(tan, res); } } ans += tan; std::cout << ans << "\n"; return 0; }

Compilation message (stderr)

Palembang_Bridges.cpp: In function 'void vec_out(const std::vector<int>&)':
Palembang_Bridges.cpp:14:15: warning: unused variable 'x' [-Wunused-variable]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...