Submission #261905

#TimeUsernameProblemLanguageResultExecution timeMemory
261905kdh9949Wiring (IOI17_wiring)C++17
13 / 100
73 ms23904 KiB
#include "wiring.h" #include <bits/stdc++.h> using namespace std; using ll = long long; struct Pos { ll x; int t; }; ll min_total_length(vector<int> r, vector<int> b) { vector<Pos> a; a.push_back({-1LL, -1}); for(int x : r) a.push_back({x, 0}); for(int x : b) a.push_back({x, 1}); sort(a.begin(), a.end(), [](Pos &u, Pos &v){ return u.x < v.x; }); int n = int(a.size()) - 1; const static ll INF = ll(1e18); vector<ll> d(n + 1, 0), s(n + 1, 0); for(int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].x; for(int i = 1; a[i].t == a[1].t; i++) d[i] = INF; vector<ll> pm(n + 1), sm(n + 1); for(int i = 1; i <= n - 1; i++) { if(a[i].t == a[i + 1].t) continue; int L, R; for(L = i; a[L - 1].t == a[i].t; L--); for(R = i + 1; a[R + 1].t == a[i + 1].t; R++); for(int j = L; j <= i; j++) { pm[j] = d[j - 1] - 2 * s[i] + s[j - 1] - (j - 2 * i - 1) * a[i + 1].x; sm[j] = d[j - 1] - 2 * s[i] + s[j - 1] - (j - 2 * i - 1) * a[i].x; } for(int j = L + 1; j <= i; j++) pm[j] = min(pm[j], pm[j - 1]); for(int j = i - 1; j >= L; j--) sm[j] = min(sm[j], sm[j + 1]); for(int j = i + 1; j <= R; j++) { int B = max(L - 1, 2 * i - j); d[j] = min( sm[B + 1] + s[j] - j * a[i].x, (B < L ? INF : pm[B] + s[j] - j * a[i + 1].x) ); } } return d[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...