제출 #435551

#제출 시각아이디문제언어결과실행 시간메모리
435551sinamhdvPalembang Bridges (APIO15_bridge)C++11
100 / 100
100 ms3720 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int mod = 1000 * 1000 * 1000 + 7; const int INF = 1e9 + 100; const ll LINF = 1e18 + 100; #ifdef DEBUG #define dbg(x) cout << #x << " = " << (x) << endl << flush; #define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; } #else #define dbg(x) ; #define dbgr(s, f) ; #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); i++) #define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define fr first #define sc second #define endl '\n' #define MAXN 100100 int k, n; int lft[MAXN], rgt[MAXN]; char z1[MAXN], z2[MAXN]; ll ans, res[MAXN]; vector<int> vec; priority_queue<int> pql; priority_queue<int, vector<int>, greater<int>> pqr; ll lsum, rsum; void add(int x) { if (pqr.size() && x >= pqr.top()) pqr.push(x), rsum += x; else pql.push(x), lsum += x; if (pqr.size() > pql.size()) { pql.push(pqr.top()); lsum += pqr.top(); rsum -= pqr.top(); pqr.pop(); } else if (pql.size() > pqr.size() + 1) { pqr.push(pql.top()); lsum -= pql.top(); rsum += pql.top(); pql.pop(); } } inline ll getres(void) { ll mid = pql.top(); return mid * pql.size() - lsum + rsum - mid * pqr.size(); } int32_t main(void) { fast_io; cin >> k >> n; FOR(i, 0, n) { cin >> z1[i] >> lft[i] >> z2[i] >> rgt[i]; if (lft[i] > rgt[i]) swap(lft[i], rgt[i]); if (z1[i] == z2[i]) ans += rgt[i] - lft[i]; else vec.pb(i), ans++; } sort(all(vec), [&](int x, int y){ return (lft[x] + rgt[x]) < (lft[y] + rgt[y]); }); FOR(i, 1, vec.size()) { add(lft[vec[i - 1]]); add(rgt[vec[i - 1]]); res[i] += getres(); } // clear while (pql.size()) pql.pop(); while (pqr.size()) pqr.pop(); lsum = rsum = 0; for (int i = (int)vec.size() - 1; i >= 0; i--) { add(lft[vec[i]]); add(rgt[vec[i]]); res[i] += getres(); } if (k == 2) cout << *min_element(res, res + vec.size()) + ans << endl; else cout << res[0] + ans << endl; return 0; }
#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...