이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 61;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if(r.back() < b.front()) {
int rn = r.size(), bn = b.size();
long long ans = 0;
for(int i : r) ans -= i;
for(int i : b) ans += i;
if(rn > bn) ans += (long long)b.front() * (rn - bn);
if(rn < bn) ans -= (long long)r.back() * (bn - rn);
return ans;
}
int n = r.size() + b.size();
vector<pair<int, int> > v;
for(int i : r) v.push_back(make_pair(i, 0));
for(int i : b) v.push_back(make_pair(i, 1));
sort(v.begin(), v.end());
vector<long long> dp(2 * n + 1, inf);
dp[n] = 0;
for(pair<int, int> i : v) {
vector<long long> ndp(2 * n + 1, inf);
if(i.second == 1) {
long long opt = inf;
for(int j = 0; j <= 2 * n; ++j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
else {
long long opt = inf;
for(int j = 2 * n; j >= 0; --j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
dp = ndp;
}
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... |