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"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(int i=a; i<b; i++)
typedef vector<int>vi;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
typedef pair<int,int>pi;
typedef vector<pi>vpi;
#define fi first
#define se second
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const ll INF=1e18;
//------------------------------------------
int N, M;
vi r,b;
ll dist(int i, int j){return abs(b[j]-r[i]);}
int check(vi a, vi b){
for(int x: a) if(!x) return 0;
for(int x: b) if(!x) return 0;
return 1;
}
ll min_total_length(vi r, vi b) {
::r=r; ::b=b;
N=sz(r), M=sz(b);
vector<ll> reqr(N,INF), reqb(M,INF);
FOR(i,0,N) FOR(j,0,M) ckmin(reqr[i],dist(i,j)), ckmin(reqb[j],dist(i,j));
ll ans=0;
FOR(i,0,N) ans+=reqr[i];
FOR(j,0,M) ans+=reqb[j];
set<pair<int,pi>,greater<pair<int,pi>>>s;
FOR(i,0,N) FOR(j,0,M) s.insert({reqr[i]+reqb[j]-dist(i,j),{i,j}});
vi visr(N,0), visb(M,0);
while(!check(visr,visb)){
auto it=s.begin();
int val=(*it).fi, i=(*it).se.fi, j=(*it).se.se;
ans-=val; visr[i]=1; visb[j]=1; reqr[i]=0; reqb[j]=0;
s.clear();
FOR(i,0,N) FOR(j,0,M) s.insert({reqr[i]+reqb[j]-dist(i,j),{i,j}});
}
return ans;
/*ll ans=0;
FOR(i,0,N){
ll dif=-1; int idx;
FOR(j,0,M) if(ckmax(dif,reqr[i]+reqb[j]-dist(i,j))) idx=j;
ans+=dist(i,idx);
reqr[i]=0; reqb[idx]=0;
}
FOR(j,0,M) if(reqb[j]){
ll dif=-1; int idx;
FOR(i,0,N) if(ckmax(dif,reqr[i]+reqb[j]-dist(i,j))) idx=i;
ans+=dist(idx,j);
}
return ans; */
}
/*
4 5
1 2 3 7
0 4 5 9 10
10
*/
# | 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... |