Advertisement
tepyotin2

Meeting on the Line

Jul 4th, 2025
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long double ld;
  6. const ld EPS = 1e-7;
  7.  
  8. int T;
  9. int n;
  10. vector<ld> x, t;
  11.  
  12. ld cost(ld pos){
  13.     ld mx = 0;
  14.     for(int i=0; i<n; i++){
  15.         // cout << "x[i]: " << x[i] << ", t[i]: " << t[i] << '\n';
  16.         mx = max(mx, t[i]+abs(x[i]-pos));
  17.     }
  18.     return mx;
  19. }
  20.  
  21. int main(){
  22.     // freopen("meetonline.in", "r", stdin);
  23.     // freopen("meetonline1.out", "w", stdout);
  24.  
  25.     cin >> T;
  26.     while(T--){
  27.         cin >> n;
  28.         // vector<ld> x(n), t(n);
  29.         x.resize(n, 0);
  30.         t.resize(n, 0);
  31.         for(int i=0; i<n; i++){
  32.             cin >> x[i];
  33.             // cout << x[i] << " ";
  34.         }
  35.         // cout << '\n';
  36.         for(int i=0; i<n; i++){
  37.             cin >> t[i];
  38.             // cout << t[i] << " ";
  39.         }
  40.         // cout << '\n';
  41.         ld lo = 0, hi = 1e8;
  42.         for(int v=0; v<100; v++){
  43.             ld m1 = lo+(hi-lo)/3;
  44.             ld m2 = hi-(hi-lo)/3;
  45.             // cout << "v: " << v << ", lo: " << lo << ", hi: " << hi << ", m1: " << m1 << ", m2: " << m2 << '\n';
  46.             // cout << "cm1: " << cost(m1) << ", cm2: " << cost(m2) << '\n';
  47.             if(cost(m1)<cost(m2)){
  48.                 hi = m2;
  49.             }else{
  50.                 lo = m1;
  51.             }
  52.         }
  53.         cout << fixed << setprecision(10) << (lo+hi)/2 << '\n';
  54.     }
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement