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"
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
#include <limits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, int> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
// #define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
long long min_total_length(vi r, vi b) {
int n = r.size(), m = b.size();
vector<pair<int,pi>> arr;
FOR(i,0,n)arr.pb({r[i],{0,i}});
FOR(i,0,m)arr.pb({b[i],{1,i}});
vector<priority_queue<pi>> con(n+m);
sort(arr.begin(),arr.end());
int lastr = -1,lastb = -1;
FOR(i,0,n+m){
if(lastr != -1 && lastb != -1)break;
if(lastr == -1 && arr[i].S.F == 0)lastr = i;
if(lastb == -1 && arr[i].S.F == 0)lastb = i;
}
ll ans = 0;
FOR(i,0,arr.size()){
pi edge = arr[i].S;
cerr << endl << arr[i].F << " " << (edge.F ? "blue" : "red") << " " << edge.S << " | " << con[i].size() << " - " << endl;
if(con[i].size()>0)continue;
if(edge.F == 1){
int dis = abs(arr[lastr].F-arr[i].F);
ans += dis;
if(con[lastr].size() > 0){
pi node = con[lastr].top();
if(con[node.S].size() > 1){
cerr << "remove " << node.F << " | " << arr[lastr].F << " " << node.S << endl;
con[lastr].pop();
ans -= node.F;
}
}
con[lastr].push({dis,i});
con[i].push({dis,lastr});
cerr << "connect " << dis << " | " << arr[i].F << " & " << arr[lastr].F << endl;
lastb = i;
}else{
int dis = abs(arr[lastb].F-arr[i].F);;
ans += dis;
if(con[lastb].size() > 0){
pi node = con[lastb].top();
if(con[node.S].size() > 1){
cerr << "remove " << arr[lastb].F << " " << node.S << endl;
con[lastb].pop();
ans -= node.F;
}
}
con[lastb].push({dis,i});
con[i].push({dis,lastb});
cerr << "connect " << dis << " | " << arr[i].F << " & " << arr[lastb].F << endl;
lastr = i;
}
}
return ans;
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(vi, vi)':
wiring.cpp:24:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | #define FOR(i, a, b) for (int i = a; i < (b); i++)
| ^
wiring.cpp:62:2: note: in expansion of macro 'FOR'
62 | FOR(i,0,arr.size()){
| ^~~
# | 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... |