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 <vector>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
ll min_total_length(vector<int> r, vector<int> b)
{
/*vector<vector<ll>> dp(r.size(), vector<ll>(b.size()));
for (int i = 0; i < b.size(); ++i)
{
dp[0][i] = abs(r[0] - b[i]);
if (i != 0) dp[0][i] += dp[0][i - 1];
}
for (int i = 1; i < r.size(); ++i)
{
for (int j = 0; j < b.size(); ++j)
{
dp[i][j] = dp[i - 1][j] + abs(r[i] - b[j]);
if (j != 0)
{
dp[i][j] = min({dp[i][j], dp[i][j - 1] + abs(r[i] - b[j]),
dp[i - 1][j - 1] + abs(r[i] - b[j])});
}
}
}
return dp.back().back();*/
vector<pair<ll, bool>> points;
for (int i : r) points.emplace_back(i, 0);
for (int i : b) points.emplace_back(i, 1);
sort(points.begin(), points.end());
int n = points.size();
vector<ll> dp(n, 1e18);
multiset<ll> prev, prevCh;
vector<ll> cur, help;
cur.push_back(0); //change maybe later
ll sum = 0, sumPrev = 0, sumCur = 0;
int cntPrev = 0, cntCur = 1;
for (int i = 1; i < n; ++i)
{
if (points[i].second != points[i - 1].second)
{
cntPrev = cntCur;
cntCur = 0;
sumPrev = sumCur;
sumCur = sum = 0;
prev.clear();
prevCh.clear();
for (int j = 0; j < cur.size(); ++j)
prev.insert(cur[j] + (cntPrev - j) * (points[i].first - points[i - 1].first));
help = cur;
reverse(help.begin(), help.end());
cur.clear();
//change
//add delta
}
//do addition
++cntCur;
sum += points[i].first - points[i - cntCur + 1].first;
sumCur += (cntCur - 1) * (points[i].first - points[i - 1].first);
cur.push_back(dp[i - 1] + sumCur);
//change in prev because cntCur > cnt there
if (prev.empty() && prevCh.empty()) continue;
dp[i] = dp[i - cntCur] + cntCur * (points[i - cntCur + 1].first - points[i - cntCur].first) + sum;
if (!prev.empty()) dp[i] = min(dp[i], *prev.begin() + sum);
if (!prevCh.empty()) dp[i] = min(dp[i], *prevCh.begin() + sum + cntCur * (points[i - cntCur + 1].first - points[i - cntCur].first));
if (!prev.empty()) prev.erase(prev.find(help[cntCur - 1] + cntCur * (points[i - cntCur + 1].first - points[i - cntCur].first)));
prevCh.insert(help[cntCur - 1]);
/*
4 5
1 2 3 7
0 4 5 9 10
*/
/*int last;
ll sum = 0, cnt = 1;
for (last = i - 1; last >= 0 && points[last].second == points[i].second; --last)
{
sum += (points[last + 1].first - points[last].first) * (i - last);
++cnt;
}
if (last < 0) continue;
dp[i] = dp[last] + sum + cnt * (points[last + 1].first - points[last].first);
int st = last;
ll sum2 = 0;
for (; last >= 0 && points[last].second != points[i].second; --last)
{
sum2 += points[st].first - points[last].first;
ll res = sum2 + sum + max(cnt, st - last + 1ll) * (points[st + 1].first - points[st].first);
if (last > 0) res += dp[last - 1];
dp[i] = min(dp[i], res);
}*/
}
return dp.back();
}
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:53:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int j = 0; j < cur.size(); ++j)
| ~~^~~~~~~~~~~~
wiring.cpp:41:17: warning: variable 'sumPrev' set but not used [-Wunused-but-set-variable]
41 | ll sum = 0, sumPrev = 0, sumCur = 0;
| ^~~~~~~
# | 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... |