제출 #298792

#제출 시각아이디문제언어결과실행 시간메모리
298792ChrisT전선 연결 (IOI17_wiring)C++17
30 / 100
1060 ms8324 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); int j = 0, pp = 0; for (int i = 1; i <= n; i++) { j = lst[i] - 1; if (lst[i] != lst[i-1]) pp = j; for (int k = pp; k > 0 && 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); if (att < dp[i]) { dp[i] = att; pp = k; } } } 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...