This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#include "grader.cpp"
#else
#include "wiring.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const ll INF = 1e18;
const int SIZE = 2e5 + 5;
const int K = 20;
int n, m, sz;
int a[SIZE], b[SIZE];
pair<int, int> p[SIZE];
ll dp[SIZE];
ll A[K + 1][K + 1];
ll cal(int l, int r) {
n = m = 0;
FOR (i, l, r) {
auto [x, t] = p[i];
if (t == 0) a[++n] = x;
if (t == 1) b[++m] = x;
}
if (n == 0 || m == 0) return INF;
FOR (i, 1, n) FOR (j, 1, m) {
A[i][j] = INF;
if (i > 1) A[i][j] = min(A[i][j], A[i - 1][j]);
if (j > 1) A[i][j] = min(A[i][j], A[i][j - 1]);
if (i > 1 && j > 1) A[i][j] = min(A[i][j], A[i - 1][j - 1]);
if (i == 1 && j == 1) A[i][j] = 0;
A[i][j] += abs(a[i] - b[j]);
}
return A[n][m];
}
ll min_total_length(vector<int> _r, vector<int> _b) {
n = _r.size();
m = _b.size();
sz = 0;
for (int x : _r) p[++sz] = {x, 0};
for (int x : _b) p[++sz] = {x, 1};
sort(p + 1, p + sz + 1);
FOR (i, 1, sz) {
dp[i] = INF;
FOR (j, max(0, i - K), i - 1) {
dp[i] = min(dp[i], dp[j] + cal(j + 1, i));
}
debug(i, dp[i]);
}
return dp[sz];
}
/*
in1
4 5
1 2 3 7
0 4 5 9 10
out1
10
*/
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
wiring.cpp:68:9: note: in expansion of macro 'debug'
68 | debug(i, dp[i]);
| ^~~~~
# | 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... |