G - Tuning Editorial
by
spheniscine
\((A_i+c_i)(B_j+d_j) = (A_j+c_j)(B_i+d_i) \Rightarrow \\ (A_i+c_i) : (B_i+d_i) = (A_j+c_j) : (B_j+d_j)\), [equality of ratios, where all \(x:0\) are considered equal, and \(0:0\) is uniquely considered equal to every ratio, without transitivity.]
In other words, if we plot all points \((A_i+c_i, B_i+d_i)\) on the plane, they must all lie on a single line that goes through the origin.
Consider fixing the slope of the line; the cost becomes the sum of Manhattan distances of all \((A_i, B_i)\) to the line. It can be seen that the points should only move either horizontally or vertically, and that which one solely depends on the slope (\(m\)) of the line; if \(-1 \le m \le 1\), the moves should be vertical, and horizontal otherwise.
Note that if we transpose the plane such that \((x, y) \mapsto (y, x)\) (in other words, reflection across the \(y = x\) line), slopes are swapped with their reciprocals, so slopes outside \([-1, 1]\) would now fall into \([-1, 1]\). Therefore, we may simply solve the problem for slopes within \([-1, 1]\) twice. Furthermore, we may also transpose all points whose \(A_i < 0\) by negating both \(A_i\) and \(B_i\) without changing the answer, therefore we assume \(A_i \ge 0\).
For slopes within \([-1, 1]\), the total cost will be \(\sum_i |A_i m - B_i|\). If we let \(f_i(m) = |A_i m - B_i|\), it can be seen that this decomposes to either one linear function, or two linear functions with one changepoint. To thus find the minimum sum, we first add up all the linear functions for \(m = -1\), and sort the changepoints to track changes to the linear function. The minimum must thus be either one of the changepoints or one of the two extrema (\(m = -1\) or \(1\)). The problem is thus solved in \(O(N \log N)\) time, due to needing to sort the changepoints.
Note additionally that the coefficients for the linear functions are always integers within magnitude \(O(NM)\) (where \(M\) is the maximum of \(|A_i|\) or \(|B_i|\)). Since the changepoints are rational with numerator and denominator within \(O(M)\), the answer could be a rational with a numerator within \(O(NM^2)\) and denominator \(O(M)\). This fits within \(64\)-bit signed integers; however comparing two rationals by cross-multiplication could overflow it, so you may need to use a \(128\)-bit signed integer type for the comparisons.
Sample implementation (C++):
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pl=pair<ll,ll>;
using i128=__int128;
bool comp(const pl &l, const pl &r){
return (((i128)l.first)*((i128)r.second) < ((i128)r.first)*((i128)l.second));
}
ll sgn(ll x){
if(x>0){return 1;}
else if(x<0){return -1;}
return 0;
}
pl solve(vector<ll> a,vector<ll> b){
ll n=a.size();
vector<pl> vp;
for(ll i=0;i<n;i++){
if(a[i]<0){a[i]*=-1; b[i]*=-1;}
if(a[i]>0){vp.push_back({b[i],a[i]});}
}
sort(vp.begin(),vp.end(),comp);
pl x={1,1};
ll ps=0,ss=0;
for(auto &nx : vp){ss+=nx.second;}
for(auto &nx : vp){
ll s1=sgn(ps-ss);
ps+=nx.second; ss-=nx.second;
ll s2=sgn(ps-ss);
if(s1*s2<=0){
x=nx;
break;
}
}
pl res={0,x.second};
for(ll i=0;i<n;i++){
ll lef=a[i]*x.first;
ll rig=b[i]*x.second;
res.first+=abs(lef-rig);
}
ll g=gcd(res.first,res.second);
res.first/=g;
res.second/=g;
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> a(n),b(n);
for(auto &nx : a){cin >> nx;}
for(auto &nx : b){cin >> nx;}
auto r1=solve(a,b);
auto r2=solve(b,a);
if(comp(r1,r2)){
cout << r1.first << " " << r1.second << "\n";
}
else{
cout << r2.first << " " << r2.second << "\n";
}
return 0;
}
