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;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
#define sz(v) ((int)v.size())
const int N = 2002;
const ll INF = 1e18 + 69;
int n, m;
vector<ll> dp[N];
vector<ll> bruh[N];
ll min_total_length(vector<int> a, vector<int> b) {
n = a.size(), m = b.size();
vector<pair<int, int>> point;
for(int i: a) point.push_back({i, 0});
for(int i: b) point.push_back({i, 1});
sort(ALL(point));
vector<vector<int>> component;
for(int i = 0; i<sz(point); ++i){
if (i == 0) {
component.push_back({point[i].first});
}
else{
if (point[i].second != point[i-1].second){
component.push_back({point[i].first});
}
else component.back().push_back(point[i].first);
}
}
int k = component.size();
for(int i = 0; i<k; ++i){
dp[i].resize(sz(component[i]) + 1, INF);
}
for(int i = 0; i<k; ++i){
bruh[i].resize(sz(component[i]) + 1, 0);
ll s = 0;
for(int j = 0; j < sz(component[i]); ++j){
s += abs(component[i][j] - component[i][0]);
bruh[i][j+1] += s;
}
s = 0;
for(int j = component[i].size() - 1; j>=0; --j){
s += abs(component[i].back() - component[i][j]);
bruh[i][j] += s;
}
}
for(int j = 1; j < sz(bruh[0]); ++j) bruh[0][j] = INF;
for(int j = 0; j+1 < sz(bruh[k-1]); ++j) bruh[k-1][j] = INF;
dp[0][0] = bruh[0][0];
for(int i = 1; i<k; ++i){
ll dis = (component[i][0] - component[i-1].back());
vector<ll> suff_min(sz(dp[i]), INF);
for(int t = 0; t < sz(dp[i-1]); ++t){
int r = sz(dp[i-1]) - t - 1;
ll cost = dp[i-1][t] + dis * r;
minimize(r, sz(dp[i]) - 1);
minimize(suff_min[r], cost);
}
for(int t = sz(dp[i]) - 2; t >= 0; --t) minimize(suff_min[t], suff_min[t+1]);
for(int t = 0; t < sz(dp[i]); ++t) minimize(dp[i][t], suff_min[t] + bruh[i][t]);
suff_min = dp[i-1];
for(int t = sz(suff_min) - 2; t>= 0; --t) minimize(suff_min[t], suff_min[t+1]);
for(int t = 0; t < sz(dp[i]); ++t){
ll cost = bruh[i][t] + dis * t;
int r = sz(dp[i-1]) - t - 1;
maximize(r, 0);
cost += suff_min[r];
minimize(dp[i][t], cost);
}
}
return dp[k-1].back();
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int n, m; cin >> n >> m;
// vector<int> a(n), b(m);
// for(int i = 0 ; i<n; ++i) cin >> a[i];
// for(int j = 0; j<m; ++j) cin >> b[j];
// cout << min_total_length(a, b) << "\n";
// return 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... |