This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define h1 7897897897897897
#define h2 7897466719774591
#define b1 98762051
#define b2 98765431
#define inf 1000000000
#define pi 3.1415926535897932384626
#define LMAX 9223372036854775807
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define SET(a, b) memset(a, b, sizeof(a));
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
pii p[200005];
ll dp[200005];
long long min_total_length(vector<int> a, vector<int> b) {
int n = a.size(), m = b.size();
FOR(i, 0, n-1) {
p[i] = {a[i], 0};
}
FOR(i, n, n+m-1) {
p[i] = {b[i-n], 1};
}
sort(p, p+n+m);
int red = -1, blue = -1;
FOR(i, 0, n+m-1) {
if (i > 0) dp[i] = dp[i-1];
if (p[i].se == 0) {
red++;
if (red == 0 && blue != -1) {
FOR(j, 0, blue) dp[i] += abs(a[0] - b[j]);
}
if (red <= blue && red > 0) {
dp[i] += -abs(a[red-1] - b[blue]) + abs(a[red] - b[blue]);
} else if (red > 0 && blue != -1) {
dp[i] += abs(a[red] - b[blue]);
}
} else {
blue++;
if (blue == 0 && red != -1) {
FOR(j, 0, red) dp[i] += abs(b[0] - a[j]);
}
if (blue <= red && blue > 0) {
dp[i] += -abs(b[blue-1] - a[red]) + abs(b[blue] - a[red]);
} else if (blue > 0 && red != -1) {
dp[i] += abs(a[red] - b[blue]);
}
}
//cout << dp[i] << endl;
}
return dp[n+m-1];
}
# | 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... |