Submission #298786

#TimeUsernameProblemLanguageResultExecution timeMemory
298786ChrisTWiring (IOI17_wiring)C++17
13 / 100
1049 ms8168 KiB
#include <bits/stdc++.h> #include "wiring.h" using namespace std; const int MN = 2e5 + 3; long long min_total_length (vector<int> r, vector<int> b) { sort(r.begin(),r.end()); sort(b.begin(),b.end()); if (r.back() < b[0]) { long long lSum = 0, rSum = 0; for (int i : r) lSum += i; for (int i : b) rSum += i; return rSum - (long long)b[0] * ((int)b.size()) + (long long)r.back() * ((int)r.size()) - lSum + (long long)(b[0] - r.back()) * max((int)b.size(),(int)r.size()); } vector<pair<int,int>> v = {{-1,-1}}; for (int i : r) v.emplace_back(i,0); for (int i : b) v.emplace_back(i,1); int n = (int)v.size() - 1; sort(v.begin(),v.end()); vector<long long> dp(n + 1,1e18), psa(n+1); dp[0] = 0; vector<int> lst(n+1); for (int i = 1; i <= n; i++) lst[i] = i == 1 || v[i].second != v[i-1].second ? i : lst[i-1]; for (int i = 1; i <= n; i++) psa[i] = psa[i-1] + v[i].first; vector<int> des(n+1); for (int i = 1; i <= n; i++) { int j = lst[i] - 1; for (int k = j; k >= lst[j]; k--) { long long att = min(dp[k],dp[k-1]) + psa[i] - psa[j] - (long long)v[j+1].first * (i - j) + (long long)v[j].first * (j - k + 1) - (psa[j] - psa[k-1]) + (long long)(v[j+1].first - v[j].first) * max(j-k+1,i-j); dp[i] = min(dp[i],att); } } return dp[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...