제출 #332409

#제출 시각아이디문제언어결과실행 시간메모리
332409syyPalembang Bridges (APIO15_bridge)C++17
100 / 100
107 ms11116 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<ll, pi> ipi; typedef pair<pi, pi> pipi; #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define size(v) (ll) v.size() #define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin()); #define INF (ll) 1e9 + 100 #define LLINF (ll) 1e18 #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss struct seg { ll a, b, mid; seg (ll x, ll y) { a = x, b = y, mid = (a+b)/2; } bool operator<(seg x) const { return mid < x.mid; } }; ll k, n, m, a, b, ans, val[2][100005], lsum, rsum; char x, y; vector<seg> v; vi coord; priority_queue<ll> L; priority_queue<ll, vi, greater<ll>> R; int main() { fastio; cin >> k >> n; ans = n; FOR(i, 1, n) { cin >> x >> a >> y >> b; if (x == y) ans += abs(b-a) - 1; else { v.pb(seg(a, b)); coord.pb(a); coord.pb(b); } } if (v.empty()) { cout << ans; return 0; } m = size(v); v.pb(seg(-1, -1)); sort(all(v)); v.pb(seg(-1, -1)); if (k == 1) { sort(all(coord)); ll x = coord[size(coord)/2]; for (auto it:coord) ans += abs(it - x); cout << ans; return 0; } FOR(temp, 0, 1) { while (!L.empty()) L.pop(); while (!R.empty()) R.pop(); lsum = rsum = 0; FOR(i, 1, m) { a = v[i].a, b = v[i].b; if (L.empty()) { L.push(min(a, b)), R.push(max(a, b)); lsum += min(a, b), rsum += max(a, b); } else { ll med = L.top(); if (a <= med) L.push(a), lsum += a; else R.push(a), rsum += a; if (b <= med) L.push(b), lsum += b; else R.push(b), rsum += b; } while (size(R) < size(L)) { rsum += L.top(); lsum -= L.top(); R.push(L.top()); L.pop(); } while (size(L) < size(R)) { lsum += R.top(); rsum -= R.top(); L.push(R.top()); R.pop(); } assert(size(R) == size(L)); ll med = L.top(); val[temp][i] = med * size(L) - lsum + rsum - med * size(R); } reverse(all(v)); } ll ans2 = LLINF; FOR(i, 0, m) ans2 = min(ans2, val[0][i] + val[1][m-i]); cout << ans + ans2; }
#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...