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>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
using namespace std;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
using ll = long long;
constexpr ll inf = 1e18;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int N = r.size(), M = b.size();
vector<pair<ll, int>> A(N + M);
rep (i, N) A[i] = {r[i], 0};
rep (i, M) A[N + i] = {b[i], 1};
sort(all(A));
vector<int> nxt(N + M + 1);
nxt[N + M] = N + M;
nxt[N + M - 1] = N + M;
rrep (i, N + M - 1) {
if (A[i].second == A[i + 1].second) nxt[i] = nxt[i + 1];
else nxt[i] = i + 1;
}
vector<ll> dp(N + M + 1, inf);
dp[0] = 0;
rep (i, N + M) {
int m = nxt[i];
int r = nxt[m];
if (m != r) {
rep2 (j, m, r) {
if (m - i >= r - m) {
ll sm = 0;
rep2 (k, i, m) sm += (A[k + 1].first - A[k].first) * (k - i + 1);
rep2 (k, m, j) sm += (A[k + 1].first - A[k].first) * (j - k);
chmin(dp[j + 1], dp[i] + sm);
}
else {
ll sm = 0;
rep2 (k, i, m - 1) sm += (A[k + 1].first - A[k].first) * (k - i + 1);
rep2 (k, m - 1, j) sm += (A[k + 1].first - A[k].first) * (j - k);
chmin(dp[j + 1], dp[i] + sm);
}
}
}
// if (i != 0 && A[i].second != A[i - 1].second) {
// rep2 (j, i, m) {
// ll sm = 0;
// rep2 (k, i - 1, j) sm += (A[k + 1].first - A[k].first) * (j - k);
// chmin(dp[j + 1], dp[i] + sm);
// }
// }
}
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... |