Submission #989545

#TimeUsernameProblemLanguageResultExecution timeMemory
989545aykhnPalembang Bridges (APIO15_bridge)C++17
22 / 100
32 ms10700 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define inf 0x3F3F3F3F3F3F3F3F signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int k, n, S = 0; cin >> k >> n; vector<int> id, p, s, bp, bs; for (int i = 0; i < n; i++) { char a, b; int l, r; cin >> a >> l >> b >> r; if (l > r) swap(l, r); if (a == b) S += r - l; else id.push_back(l), id.push_back(r); } n = id.size(); if (n <= k) { cout << S << '\n'; return 0; } sort(id.begin(), id.end()); p = s = id; for (int i = 1; i < n; i++) p[i] += p[i - 1]; for (int i = n - 2; i >= 0; i--) s[i] += s[i + 1]; bp.assign(n, 0), bs.assign(n, 0); int res = inf; for (int i = 0; i < n; i++) { res = min(res, s[i + 1] - p[i] + id[i] * (n - 2 * (n - i - 1))); } cout << res + S + n / 2 << '\n'; } // bp[i] = s[i + 1] - p[i] + id[i] * (2 * (i + 1) - x) // bs[i] = s[i + 1] - p[i] + id[i] * (x - 2 * (n - i - 1))
#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...