Official

G - Tuning Editorial by en_translator


What does \((A_i+c_i)(B_j+d_j) = (A_j+c_j)(B_i+d_i)\) imply?
This equation is equivalent to \((A_i+c_i) : (B_i+d_i) = (A_j+c_j) : (B_j+d_j)\).

In other words, all points \((A_i+c_i, B_i+d_i)\) lies on a single line passing through the origin on the \(xy\) plane.

We can further reinterpret that the problem as follows: given points \((A_i,B_i)\), perform the following operations zero or more times to move the points to a line passing through the origin, with the minimum cost.

  • Move the point in either direction parallel to the \(x\)-axis by \(c\) for a cost \(c\).
  • Move the point in either direction parallel to the \(y\)-axis by \(d\) for a cost \(d\).

Fix the slope of the line.
We notice that at least one of the following can be satisfied:

  • \(c_i=0\) for all \(i\).
  • \(d_i=0\) for all \(i\).

For example, if the slope is fixed to \(2\), it is optimal to use \(x\)-direction moves regardless of the initial location of the point. On the other hand, if the slope is fixed to \(1/2\), using only \(y\) direction is optimal.
Intuitively, the more perpendicular to the slope the moving direction is, the faster we can reach the line.

Now we will assume that \(c_i=0\) for all \(i\). We can solve this problem twice, once with \(A\) and \(B\) swapped, and report the smaller as the answer.
Additionally, if \(A_i < 0\), multiply \(-1\) to \(A_i\) and \(B_i\) to make \(A_i \ge 0\) (which does not change the answer).

For the fixed slope \(x\), the total distance the points need to travel is \(\sum_i |A_ix-B_i|\), which we want to minimize.
Each term in this function has a descending slope \(A_i\) up to \(x \le \frac{B_i}{A_i}\), then has an ascending slope \(A_i\) afterward.
Thus, by sorting the points in ascending order of \(\frac{B_i}{A_i}\), and setting \(x=\frac{B_j}{A_j}\) for the \(j\) where the larger one between the prefix and suffix sum of \(A_i\) switches from one to another, the minimum sum is achieved.
One may notice that \(A_i=0\) invokes zero divisions, but this can be avoided because the minimum distance for such points is independent of \(x\).

This solution runs in a total of \(O(N \log N)\) time. While \(p\) and \(q\) in the answer do fit in a \(64\)-bit signed integer type, intermediate comparisons of rational integers may require arithmetic operations exceeding even a \(64\)-bit signed integer type. In case of C++, we may instead use integer types that can handle more digits, for example, __int128.

Sample code (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;
}

Original proposer: admin