이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "wiring.h"
//#include "grader.cpp"
#define pii pair < int, int >
#define fr first
#define sc second
#define ll long long
#define pb push_back
#define sz(s) (int)s.size()
using namespace std;
const int N = (int)2e5 + 7;
const ll linf = (ll)1e18 + 7;
pii a[N];
int n, m;
ll dp[N];
int last[N];
ll pref[N];
ll min_total_length(vector<int> R, vector<int> B) {
vector < ll > r, b;
n = sz(R);
m = sz(B);
for (int i = 0; i < n; i++) r.pb(R[i]);
for (int i = 0; i < m; i++) b.pb(B[i]);
int i, j;
i = j = 0;
while (i < n && j < m) {
if (r[i] < b[j]) {
a[i + j + 1] = {r[i], 1};
i++;
} else {
a[i + j + 1] = {b[j], 0};
j++;
}
}
while (i < n) a[i + j + 1] = {r[i], 1}, i++;
while (j < m) a[i + j + 1] = {b[j], 0}, j++;
for (int i = 1; i <= n + m; i++)
dp[i] = linf;
memset(last, -1, sizeof last);
int balance = 1e5;
last[balance] = 0;
j = 0;
int q = 0;
ll lastred, lastblue;
lastred = lastblue = -linf;
b.pb(linf);
r.pb(linf);
for (int i = 1; i <= n + m; i++) {
ll x = a[i].fr;
ll k = a[i].sc;
balance += (k + k - 1);
pref[i] = pref[i - 1] + ((k == 1) ? -x : x);
if (k == 1) {
while (j < m && b[j] < x) j++;
dp[i] = dp[i - 1] + min(b[j] - x, x - lastblue);
} else {
while (q < n && r[q] < x) q++;
dp[i] = dp[i - 1] + min(r[q] - x, x - lastred);
}
if (last[balance] != -1) {
dp[i] = min(dp[i], dp[last[balance]] + abs(pref[i] - pref[last[balance]]));
}
if (k == 1) lastred = x;
else lastblue = x;
last[balance] = i;
}
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... |