Submission #521496

#TimeUsernameProblemLanguageResultExecution timeMemory
521496SharkyPalembang Bridges (APIO15_bridge)C++17
63 / 100
2059 ms10028 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define N 100005 #define sz(x) x.size() #define pq priority_queue int pref[N], suff[N]; bool cmp(pair<int, int> x, pair<int, int> y) { return x.first + x.second < y.first + y.second; } void precompute(vector<pair<int, int> > pv) { int nn; pq<int> below; pq<int, vector<int>, greater<int> > above; for (int i = 1; i < pv.size(); i++) { int n = pv[i].first; if (below.empty()) { below.push(n); nn = n; } else if (above.empty()) { above.push(n); pref[i] = below.top(); nn = (above.top() + below.top()) / 2; if (above.top() < below.top()) { int tmp = below.top(); int tmp2 = above.top(); below.pop(); below.push(tmp2); above.pop(); above.push(tmp); } } else { if (n <= nn) { below.push(n); } else { above.push(n); } if (sz(below) > sz(above) + 1) { int tmp = below.top(); below.pop(); above.push(tmp); } else if (sz(below) < sz(above)) { int tmp = above.top(); above.pop(); below.push(tmp); } nn = below.top(); } n = pv[i].second; if (below.empty()) { below.push(n); nn = n; } else if (above.empty()) { above.push(n); pref[i] = below.top(); nn = (above.top() + below.top()) / 2; if (above.top() < below.top()) { int tmp = below.top(); int tmp2 = above.top(); below.pop(); below.push(tmp2); above.pop(); above.push(tmp); } } else { if (n <= nn) { below.push(n); } else { above.push(n); } if (sz(below) > sz(above) + 1) { int tmp = below.top(); below.pop(); above.push(tmp); } else if (sz(below) < sz(above)) { int tmp = above.top(); above.pop(); below.push(tmp); } nn = below.top(); pref[i] = nn; } } while (below.size()) below.pop(); while (above.size()) above.pop(); for (int i = pv.size() - 1; i >= 1; i--) { int n = pv[i].first; if (below.empty()) { below.push(n); nn = n; } else if (above.empty()) { above.push(n); suff[i] = below.top(); nn = (above.top() + below.top()) / 2; if (above.top() < below.top()) { int tmp = below.top(); int tmp2 = above.top(); below.pop(); below.push(tmp2); above.pop(); above.push(tmp); } } else { if (n <= nn) { below.push(n); } else { above.push(n); } if (sz(below) > sz(above) + 1) { int tmp = below.top(); below.pop(); above.push(tmp); } else if (sz(below) < sz(above)) { int tmp = above.top(); above.pop(); below.push(tmp); } nn = below.top(); } n = pv[i].second; if (below.empty()) { below.push(n); nn = n; } else if (above.empty()) { above.push(n); pref[i] = below.top(); nn = (above.top() + below.top()) / 2; if (above.top() < below.top()) { int tmp = below.top(); int tmp2 = above.top(); below.pop(); below.push(tmp2); above.pop(); above.push(tmp); } } else { if (n <= nn) { below.push(n); } else { above.push(n); } if (sz(below) > sz(above) + 1) { int tmp = below.top(); below.pop(); above.push(tmp); } else if (sz(below) < sz(above)) { int tmp = above.top(); above.pop(); below.push(tmp); } nn = below.top(); suff[i] = nn; } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int sameside = 0, k, n, m = 0, ans = 0; cin >> k >> n; vector<pair<int, int> > pv; pv.push_back({0, 0}); vector<int> v(2 * n + 5); for (int i = 1; i <= n; i++) { char p, q; int s, t; cin >> p >> s >> q >> t; if (p == q) sameside += abs(s - t); else { if (s > t) swap(s, t); m++; v[m] = s; m++; v[m] = t; pv.push_back({s, t}); } } sort(pv.begin(), pv.end(), cmp); precompute(pv); if (k == 1 || m == 2) { sort(v.begin() + 1, v.begin() + m + 1); int bridge = v[(m + 1) / 2]; for (int i = 1; i <= m; i += 2) { ans += abs(bridge - v[i]) + abs(bridge - v[i + 1]) + 1; } cout << ans + sameside << "\n"; } else { int mn = 1e18; for (int i = 2; i <= (m / 2); i++) { ans = 0; for (int j = 1; j <= i - 1; j++) { ans += abs(pref[i - 1] - pv[j].first) + abs(pref[i - 1] - pv[j].second) + 1; } for (int j = i; j <= (m / 2); j++) { ans += abs(suff[i] - pv[j].first) + abs(suff[i] - pv[j].second) + 1; } mn = min(mn, ans); } if (m == 0) mn = 0; cout << mn + sameside << "\n"; } return 0; } /* 2 7 B 1 A 0 A 0 B 0 B 1 B 1 B 1 A 1 B 0 A 1 B 1 A 0 B 0 A 1 */

Compilation message (stderr)

bridge.cpp: In function 'void precompute(std::vector<std::pair<long long int, long long int> >)':
bridge.cpp:14:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 1; i < pv.size(); i++) {
      |                     ~~^~~~~~~~~~~
bridge.cpp:29:13: warning: 'nn' may be used uninitialized in this function [-Wmaybe-uninitialized]
   29 |             if (n <= nn) {
      |             ^~
#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...