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];
ll dp[SIZE], pre[SIZE];
pair<int, int> p[SIZE], seg[SIZE];
ll A[205][205];
ll brute(vector<int> _r, vector<int> _b) {
n = _r.size();
m = _b.size();
FOR (i, 1, n) a[i] = _r[i - 1];
FOR (i, 1, m) b[i] = _b[i - 1];
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);
n = 0;
for (int i = 1, l = 0; i <= sz; i++) {
pre[i] = pre[i - 1] + p[i].F;
if (l == 0) l = i;
if (i == sz || p[i].S != p[i + 1].S) {
seg[++n] = {l, i};
l = 0;
}
}
fill(dp + 1, dp + sz + 1, INF);
FOR (i, 2, n) {
auto [l, r] = seg[i];
auto [pl, pr] = seg[i - 1];
debug(i, pl, pr, l, r);
multiset<ll> ms;
ll mn = INF;
FOR (j, pl, pr) ms.insert(min(dp[j - 1], dp[j]) + 1ll * (pr - j + 1) * p[l].F - (pre[pr] - pre[j - 1]));
FOR (j, l, r) {
int cnt = j - l + 1;
if (pr - pl + 1 >= cnt) mn = min(mn, min(dp[pr - cnt], dp[pr - cnt + 1]) + 1ll * cnt * p[pr].F - (pre[pr] - pre[pr - cnt]));
dp[j] = min(dp[j], mn + (pre[j] - pre[l - 1]) - 1ll * cnt * p[pr].F);
if (ms.size()) dp[j] = min(dp[j], *ms.begin() + (pre[j] - pre[l - 1]) - 1ll * cnt * p[l].F);
if (pr - pl + 1 >= cnt) ms.erase(ms.find(min(dp[pr - cnt], dp[pr - cnt + 1]) + 1ll * cnt * p[l].F - (pre[pr] - pre[pr - cnt])));
}
}
FOR (i, 1, sz) debug(i, dp[i]);
return dp[sz];
}
/*
in1
4 5
1 2 3 7
0 4 5 9 10
out1
10
1 4
2
1 6 7 8
*/
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:74:9: note: in expansion of macro 'debug'
74 | debug(i, pl, pr, l, r);
| ^~~~~
wiring.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
wiring.cpp:86:20: note: in expansion of macro 'debug'
86 | FOR (i, 1, sz) 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... |