This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "wiring.h"
namespace TEST2 {
ll solve(VI a, VI b) {
ll ans = 0;
int l = a.size()-1;
int r = 0;
while (l >= 0 && r < b.size()) {
ans += b[r] - a[l];
l--;
r++;
}
while (l >= 0) ans += b[0] - a[l--];
while (r < b.size()) ans += b[r++] - a.back();
return ans;
}
};
namespace TEST1 {
int mindist(VI::iterator begin, VI::iterator end, int x) {
int ret = 2e9;
auto it = upper_bound(begin, end, x);
if (it != end) setmin(ret, *it - x);
if (it != begin) setmin(ret, x - *prev(it));
/*cout << "mindist(" << x << ") in [";*/
/*for (VI::iterator it = begin; it != end; it++) cout << *it << " ";*/
/*cout << "\b]\t = " << ret << endl;*/
return ret;
}
ll solve(VI a, VI b) {
int n = a.size(), m = b.size();
VVL dp(n+1, VL(m+1, 1e18));
dp[0][0] = 0;
replr(i, 1, n) {
replr(j, 1, m) {
dp[i][j] = min({
dp[i-1][j-1] + abs(a[i-1] - b[j-1]),
dp[i-1][j] + mindist(b.begin(), b.begin()+j, a[i-1]),
dp[i][j-1] + mindist(a.begin(), a.begin()+i, b[j-1])
});
}
}
return dp[n][m];
}
};
ll min_total_length(VI A, VI B) {
if (A.back() < B[0]) return TEST2::solve(A, B);
return TEST1::solve(A, B);
}
Compilation message (stderr)
wiring.cpp: In function 'll TEST2::solve(VI, VI)':
wiring.cpp:39:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while (l >= 0 && r < b.size()) {
| ~~^~~~~~~~~~
wiring.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while (r < b.size()) ans += b[r++] - a.back();
| ~~^~~~~~~~~~
# | 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... |