Submission #229112

#TimeUsernameProblemLanguageResultExecution timeMemory
229112parsa_mobedPalembang Bridges (APIO15_bridge)C++14
22 / 100
103 ms15440 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) v.begin(), v.end() #define sz(v) (int)v.size() #define pii pair <int , int> #define F first #define S second #define L first #define R second const int N = 2e5 + 5, M = 2e3 + 5, inf = 1e18; int prf[N], suf[N], prf_cost[N], suf_cost[N], ss[N], ps[N], cost[M][M], cnt[M][M]; vector <int> P; vector <pii> seg; int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, k, ans = 0; cin >> k >> n; for (int i = 0; i < n; i++) { char s, e; int x1, x2; cin >> s >> x1 >> e >> x2; if (x1 > x2) swap(x1, x2); ans += x2 - x1; if (s != e) ans++, P.push_back(x1), P.push_back(x2), seg.push_back({x1, x2}); } sort(all(P)), P.resize(unique(all(P)) - P.begin()); int m = sz(P); for (pii p : seg) { int l = lower_bound(all(P), p.L) - P.begin(), r = lower_bound(all(P), p.R) - P.begin(); suf[l]++, prf[r]++, ss[l] += p.L, ps[r] += p.R; } for (int i = m - 1; i >= 0; i--) suf[i] += suf[i + 1], ss[i] += ss[i + 1]; for (int i = 1; i < m; i++) prf[i] += prf[i - 1], ps[i] += ps[i - 1]; int res = inf; for (int i = 0; i < m; i++) prf_cost[i] = P[i] * prf[i] - ps[i], suf_cost[i] = ss[i] - P[i] * suf[i], res = min(res, prf_cost[i] + suf_cost[i]); if (k == 2) { for (pii p : seg) { int l = lower_bound(all(P), p.L) - P.begin(), r = lower_bound(all(P), p.R) - P.begin(); cnt[l][r]++; } for (int len = 2; len <= m; len++) { for (int l = 0, r = len - 1; r < m; l++, r++) { if (len > 2) cost[l][r] = cost[l + 1][r - 1] + cnt[l + 1][r - 1]; cnt[l][r] += cnt[l][r - 1] + cnt[l + 1][r] - (l + 1 <= r - 1 ? cnt[l + 1][r - 1] : 0); } } for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) { //res = min(res, prf_cost[i] + suf_cost[j] + cost[i][j]); int x = P[i], y = P[j], sum = prf_cost[i] + suf_cost[j], c = 0; for (pii p : seg) if (x <= p.L && p.R <= y) sum += min(p.L - x, y - p.R), c++; if (sum != cost[i][j]) assert(cost[i][j] > sum); res = min(res, sum); } } if (res == inf) res = 0; cout << ans + res*2 << "\n"; }
#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...