1730B - Meeting on the Line - CodeForces Solution


binary search brute force greedy implementation math ternary search two pointers

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    x = [int(i) for i in input().split()]
    t = [int(i) for i in input().split()]
    l, r = 0, 1e10
    pos1, pos2 = 0, 1e8
    for i in range(n):
        l = max(l, t[i])
        pos1 = min(pos1, x[i])
        pos2 = max(pos2, x[i])
    eps = 1e-8
    m = 0
    while(r > l + eps):
        m = (l + r) / 2 
        p1,p2 = pos1, pos2
        for i in range(n):
            p1 = max(p1, x[i] - (m - t[i]))
            p2 = min(p2, x[i] + (m + t[i]))
        if p1 > p2:
            l = m 
        else:
            r = m 
    for i in range(n):
        pos1 = max(pos1, x[i] - (m - t[i]))
        pos2 = min(pos2, x[i] + (m - t[i]))
    x = (pos1 + pos2) / 2
    print('%.7f' % x)
T = int(input())
while(T):
    solve()
    T -= 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define fore(i, l, r) for (lli i = (l) - ((l) > (r)); i != (r) - ((l) > (r)); i += 1 - 2 * ((l) > (r)))
#define all(a) begin(a), end(a)
#define sz(a) lli(a.size())
#define pb push_back
#define f first
#define s second

#ifdef LOCAL
#include "../debug.h"
#else
#define debug(...)
#endif

using lli = long long;
using ld = long double;
using ii = pair<lli, lli>;

const lli N = 1e6 + 5;
const ld INF = 4e18 + 5;

const ld EPS = 1e-20;
#define eq(a, b) (abs((a) - (b)) <= +EPS)
#define neq(a, b) (!eq(a, b))
#define geq(a, b) ((a) - (b) >= -EPS)
#define leq(a, b) ((a) - (b) <= +EPS)
#define ge(a, b) ((a) - (b) > +EPS)
#define le(a, b) ((a) - (b) < -EPS)

lli n;
ii a[N];

template <class T>
bool umin(T& a, T b) {
  return (a = (le(a, b) ? a : b)) == b;
}

template <class T>
bool umax(T& a, T b) {
  return (a = (ge(a, b) ? a : b)) == b;
}

template <class T, class F>
pair<T, T> ternary(T lo, T hi, F f) {
  lli original_lo = lo;
  lli original_hi = hi;
  fore (it, 0, 234) {
    T m1 = lo + (hi - lo) / 3.0;
    T m2 = hi - (hi - lo) / 3.0;
    if (leq(f(m1), f(m2)))
      hi = m2;
    else
      lo = m1;
  }
  return min({make_pair(f(lo), lo), make_pair(f(hi), hi)});
}

void testCase() {
  cin >> n;
  multiset<lli> left, right;
  fore (i, 0, n) {
    cin >> a[i].f;
  }
  fore (i, 0, n) {
    cin >> a[i].s;
    right.insert(a[i].f + a[i].s);
  }
  ld ans = 0;
  ld mx = INF;
  ld prev = 0;
  ld delta = 0;
  sort(a, a + n);
  fore (i, 0, n) {
    delta += a[i].f - prev;
    prev = a[i].f;
    right.erase(right.find(a[i].f + a[i].s));
    left.insert(a[i].s - delta);
    lli mx_left = left.empty() ? 0 : *left.rbegin() + delta;
    lli mx_right = right.empty() ? 0 : *right.rbegin() - delta;
    if (umin<ld>(mx, max(mx_left, mx_right))) {
      ans = a[i].f;
    }
    if (i < n - 1 && a[i].f != a[i + 1].f) {
      debug(a[i], a[i + 1]);
      auto [best, x] = ternary<ld>(a[i].f, a[i + 1].f, [&](ld x) {
        ld cur_mx_left = mx_left + (x - a[i].f);
        ld cur_mx_right = mx_right - (x - a[i].f);
        ld mx = cur_mx_left;
        umax(mx, cur_mx_right);
        return mx;
      });
      if (umin(mx, best)) {
        debug(mx, best);
        ans = x;
      }
    }
  }
  cout << fixed << setprecision(10) << ans << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0), cout.tie(0);
  lli tc = 1;
  cin >> tc;
  while (tc--) {
    testCase();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts