이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <algorithm>
using namespace std;
typedef long long llong;
const int R = 0, B = 1;
const int inf = 1e9 + 7;
int n, m;
struct point {
int x, k;
bool operator<(const point &p) const {
return x < p.x;
}
};
point ps[200001];
llong sum[200001];
llong dp[200001];
llong min_total_length(vector<int> r, vector<int> b) {
n = r.size();
m = b.size();
int i = 0, j = 0, p = 0;
while (i < n && j < m) {
if (r[i] < b[j]) ps[++p] = { r[i++], R };
else ps[++p] = { b[j++], B };
}
while (i < n) ps[++p] = { r[i++], R };
while (j < m) ps[++p] = { b[j++], B };
int rd = -inf, bd = -inf, nc = 0, pc = 0, c = -1;
r.push_back(inf << 1);
b.push_back(inf << 1);
for (p = 1, i = j = 0; p <= n + m; ++p) {
int x = ps[p].x, k = ps[p].k;
if (k != c) {
c = k;
pc = nc;
nc = 0;
}
++nc;
sum[p] = sum[p - 1] + (k == R ? -x : x);
if (k == B) {
while (i < n && r[i] <= x) ++i;
dp[p] = dp[p - 1] + min(x - rd, r[i] - x);
}
else {
while (j < m && b[j] <= x) ++j;
dp[p] = dp[p - 1] + min(x - bd, b[j] - x);
}
if (nc <= pc) {
int t = p - (nc << 1);
dp[p] = min(dp[p], dp[t] + abs(sum[p] - sum[t]));
}
if (k == R) rd = x;
else bd = x;
}
return dp[n + m];
}
# | 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... |