이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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); dp[0] = 0;
for (int i = 1; i <= n; i++) {
int j = i; long long rSum = 0, lSum = 0;
while (j > 0 && v[j].second == v[i].second) rSum += v[j--].first;
int k = j;
while (k > 0) {
lSum += v[k].first;
dp[i] = min(dp[i],min(dp[k],dp[k-1]) + rSum - (long long)v[j+1].first * (i - j) + (long long)v[j].first * (j - k + 1) - lSum + (long long)(v[j+1].first - v[j].first) * max(j-k+1,i-j));
if (k == 1 || v[k-1].second == v[i].second) break;
--k;
}
}
return dp[n];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |