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 <bits/stdc++.h>
#include "wiring.h"
using namespace std;
const long long INF = 1e18;
vector< vector<int> > group;
vector< vector<long long> > sum, dp;
vector< pair<int, int> > vec;
void upd(long long &x, long long y) { x = min(x, y); }
inline long long get(int id, int l, int r) {
return sum[id][r] - sum[id][l - 1];
}
long long min_total_length(vector<int> r, vector<int> b) {
for (auto i : r) vec.push_back({i, 0});
for (auto i : b) vec.push_back({i, 1});
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); ++i) {
int j = i;
int id = group.size();
group.push_back(vector<int>());
sum.push_back(vector<long long>());
dp.push_back(vector<long long>());
while (j < vec.size() && vec[j].second == vec[i].second) {
group[id].push_back(vec[j++].first);
}
sum[id].assign(j - i + 1, 0);
dp[id].assign(j - i + 1, INF);
for (int k = 1; k <= j - i; ++k) {
sum[id][k] = sum[id][k - 1] + group[id][k - 1];
}
i = j - 1;
}
dp[0][0] = 0;
int n = group.size();
for (int i = 0; i < n; ++i) {
int sz = group[i].size();
for (int j = 0; j < sz; ++j) {
if (i) {
upd(dp[i][j + 1], dp[i][j] + group[i][j] - group[i - 1].back());
}
if (i != n - 1) {
upd(dp[i][j + 1], dp[i][j] + group[i + 1][0] - group[i][j]);
if (sz - j <= group[i + 1].size()) {
upd(dp[i + 1][sz - j], dp[i][j] + get(i + 1, 1, sz - j) - get(i, j + 1, sz));
}
}
}
if (i != n - 1) {
upd(dp[i + 1][0], dp[i][sz]);
}
else return dp[i][sz];
}
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); ++i) {
~~^~~~~~~~~~~~
wiring.cpp:31:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (j < vec.size() && vec[j].second == vec[i].second) {
~~^~~~~~~~~~~~
wiring.cpp:56:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (sz - j <= group[i + 1].size()) {
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
wiring.cpp:67:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | 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... |