Submission #69887760


Source Code Expand

// Problem: 'E - Closest Moment'
// Contest: 'AtCoder - AtCoder Beginner Contest 426'
// URL: 'https://atcoder.jp/contests/abc426/tasks/abc426_e'
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by competitive-companion.el (https://github.com/luishgh/competitive-companion.el)

#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

typedef long double ld;
const ld eps = 1e-9;

#define sq(x) ((x)*(x))

bool eq(ld a, ld b) {
  return abs(a - b) <= eps;
}

struct pt {
  ld x, y;
  pt(ld x_ = 0, ld y_ = 0) : x(x_), y(y_) {}

  pt operator + (const pt p) const  {return pt(x+p.x, y+p.y); }
  pt operator - (const pt p) const  {return pt(x-p.x, y-p.y); }
  pt operator * (const ld c) const  {return pt(x*c, y*c); }
  pt operator / (const ld c) const { return pt(x/c  , y/c  ); }
};

ld dist(pt p, pt q) {
  return hypot(p.y - q.y, p.x - q.x);
}

ld dist2(pt p, pt q) {
  return sq(p.x - q.x) + sq(p.y - q.y);
}


ld norm(pt v) { // norma do vetor
	return dist(pt(0, 0), v);
}

pt get_comb(pt a, pt b, ld t) {
  ld n = norm(b - a);
  if (t >= n) return b;
  pt v = (b - a) / n;
  return v * t + a;
}

ld get_dist(pt ta, pt tb, pt aa, pt ab, ld t) {
  pt tt = get_comb(ta, tb, t);
  pt at = get_comb(aa, ab, t);
  // cerr << t << " {" << tt.x << ", " << tt.y << "} {" << at.x << ", " << at.y << "}" << endl;

  return dist(tt, at);
} 

ld get_dist2(pt ta, pt tb, pt aa, pt ab, ld t) {
  pt tt = get_comb(ta, tb, t);
  pt at = get_comb(aa, ab, t);
  // cerr << t << " {" << tt.x << ", " << tt.y << "} {" << at.x << ", " << at.y << "}" << endl;

  return dist2(tt, at);
} 

void solve() {
  ll tsx, tsy, tgx, tgy;
  cin >>  tsx >>  tsy >>  tgx >>  tgy;
  ll asx, asy, agx, agy;
  cin >>  asx >>  asy >>  agx >>  agy;
  pt ta(tsx, tsy);
  pt tb(tgx, tgy);

  pt aa(asx, asy);
  pt ab(agx, agy);

  if (norm(tb - ta) < norm(ab - aa)) {
    swap(ta, aa);
    swap(tb, ab);
  }

  // ld l = get_dist2(ta, tb, aa, ab, 0);
  ld la = 0;

  ld ra = norm(ab - aa); 
  // ld r = get_dist2(ta, tb, aa, ab, ra);

  while (ra - la > eps) {
    // cerr << la << ' ' << ra << endl;
    ld m1 = la + (ra - la) / 3;
    ld m2 = ra - (ra - la) / 3;
    ld f1 = get_dist2(ta, tb, aa, ab, m1);
    ld f2 = get_dist2(ta, tb, aa, ab, m2);
    if (f1 > f2)
      la = m1;
    else
      ra = m2;
  }

  ld ans = get_dist(ta, tb, aa, ab, la);

  la = norm(ab - aa), ra = norm(tb - ta);

  while (ra - la > eps) {
    // cerr << la << ' ' << ra << endl;
    ld m1 = la + (ra - la) / 3;
    ld m2 = ra - (ra - la) / 3;
    ld f1 = get_dist2(ta, tb, aa, ab, m1);
    ld f2 = get_dist2(ta, tb, aa, ab, m2);
    if (f1 > f2)
      la = m1;
    else
      ra = m2;
  }

  ans = min(ans, get_dist(ta, tb, aa, ab, la));

  // cerr << la << endl;
  cout << ans << endl;
}

int main() {_;

  cout << fixed << setprecision(15);
  int ttt; cin >> ttt;
  while (ttt--) solve();

  return 0;
}

Submission Info

Submission Time
Task E - Closest Moment
User luishgh
Language C++ 20 (gcc 12.2)
Score 450
Code Size 3156 Byte
Status AC
Exec Time 2896 ms
Memory 4332 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 1
AC × 11
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 02_large_00.txt, 02_large_01.txt, 02_large_02.txt, 02_large_03.txt, 02_large_04.txt, 02_large_05.txt, 02_large_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3688 KiB
01_small_00.txt AC 2269 ms 3976 KiB
01_small_01.txt AC 1822 ms 3700 KiB
01_small_02.txt AC 2387 ms 3956 KiB
02_large_00.txt AC 2865 ms 4188 KiB
02_large_01.txt AC 2896 ms 4180 KiB
02_large_02.txt AC 2868 ms 4188 KiB
02_large_03.txt AC 2866 ms 4208 KiB
02_large_04.txt AC 2838 ms 4332 KiB
02_large_05.txt AC 2892 ms 4304 KiB
02_large_06.txt AC 67 ms 3772 KiB