Advertisement
i_love_rao_khushboo

B. Meeting on the Line

Oct 2nd, 2022
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.69 KB | None | 0 0
  1. // Problem: B. Meeting on the Line
  2. // Contest: Codeforces - Codeforces Round #823 (Div. 2)
  3. // URL: https://codeforces.com/contest/1730/problem/B
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. // Parsed on: 03-10-2022 07:13:28 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define pf push_front
  22. #define ppf pop_front
  23. #define mp make_pair
  24. #define F first
  25. #define S second
  26. #define PI 3.1415926535897932384626
  27. #define sz(x) ((int)(x).size())
  28. #define vset(v, n, val) v.clear(); v.resize(n, val)
  29.  
  30. typedef pair<int, int> pii;
  31. typedef pair<ll, ll> pll;
  32. typedef vector<int> vi;
  33. typedef vector<ll> vll;
  34. typedef vector<ull> vull;
  35. typedef vector<bool> vb;
  36. typedef vector<char> vc;
  37. typedef vector<string> vs;
  38. typedef vector<pii> vpii;
  39. typedef vector<pll> vpll;
  40. typedef vector<vi> vvi;
  41. typedef vector<vll> vvll;
  42. typedef vector<vull> vvull;
  43. typedef vector<vb> vvb;
  44. typedef vector<vc> vvc;
  45. typedef vector<vs> vvs;
  46.  
  47. /************************************************** DEBUGGER *******************************************************************************************************/
  48.  
  49. #ifndef ONLINE_JUDGE
  50. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  51. #else
  52. #define debug(x)
  53. #endif
  54.  
  55. void _print(ll t) { cerr << t; }
  56. void _print(int t) { cerr << t; }
  57. void _print(string t) { cerr << t; }
  58. void _print(char t) { cerr << t; }
  59. void _print(ld t) { cerr << t; }
  60. void _print(double t) { cerr << t; }
  61. void _print(ull t) { cerr << t; }
  62.  
  63. template <class T, class V> void _print(pair <T, V> p);
  64. template <class T> void _print(vector <T> v);
  65. template <class T> void _print(vector <vector<T>> v);
  66. template <class T> void _print(set <T> v);
  67. template <class T, class V> void _print(map <T, V> v);
  68. template <class T> void _print(multiset <T> v);
  69. template <class T, class V> void _print(multimap <T, V> v);
  70. template <class T> void _print(queue <T> v);
  71. template <class T> void _print(priority_queue <T> v);
  72. template <class T> void _print(stack <T> s);
  73.  
  74. // modify it's definition below as per need such as it can be used for STL containers with custom args passed
  75. template <class T> void _print(T v);
  76.  
  77. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  78. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  79. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  80. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  81. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  82. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  83. template <class T, class V> void _print(multimap <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  84. template <class T> void _print(queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.front()); v.pop(); cerr << " "; } cerr << "]"; }
  85. template <class T> void _print(priority_queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  86. template <class T> void _print(stack <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  87. template <class T> void _print(T v) {  }
  88.  
  89. /*******************************************************************************************************************************************************************/
  90.  
  91. const int INF = 0x3f3f3f3f;
  92. const int mod = 1e9+7;
  93.  
  94. const double eps = 1e-9;
  95.  
  96. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  97.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  98.                          
  99. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  100. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  101.  
  102. /******************************************************************************************************************************/
  103.  
  104. pair<bool, pair<double, double>> intersect(pair<double, double> cur_rng, pair<double, double> cur_person_range) {
  105.     if((cur_rng.F > cur_person_range.S) or (cur_rng.S < cur_person_range.F)) {
  106.         return {0, {-1, -1}};
  107.     }
  108.    
  109.     cur_rng.F = max(cur_rng.F, cur_person_range.F);
  110.     cur_rng.S = min(cur_rng.S, cur_person_range.S);
  111.    
  112.     return {1, cur_rng};
  113. }
  114.  
  115. bool good(double T, pair<double, double> &cur_rng, ll n, vll &x, vll &t) {
  116.     for(ll i = 0; i < n; i++) {
  117.         if(t[i] > T) return false;
  118.        
  119.         double time_left = T - t[i];
  120.        
  121.         pair<double, double> cur_person_range = {x[i] - time_left, x[i] + time_left};
  122.        
  123.         pair<bool, pair<double, double>> p = intersect(cur_rng, cur_person_range);
  124.         if(p.F == false) return false;
  125.         else cur_rng = p.S;
  126.     }
  127.    
  128.     return true;
  129. }
  130.  
  131. // https://www.youtube.com/watch?v=JpItlEPYrao&ab_channel=CompetitiveCoding-NewtonSchool
  132. // https://codeforces.com/blog/entry/107293
  133. void solve()
  134. {
  135.     ll n; cin >> n;
  136.    
  137.     vll x(n), t(n);
  138.    
  139.     for(ll i = 0; i < n; i++) cin >> x[i];
  140.     for(ll i = 0; i < n; i++) cin >> t[i];
  141.    
  142.     double res;
  143.    
  144.     pair<double, double> rng;
  145.    
  146.     double L = 0, R = 1e14;
  147.    
  148.     while((R - L) > eps) {
  149.         double mid = L + ((R - L) / 2.0);
  150.        
  151.         // check function begins
  152.         pair<double, double> cur_rng = {0, 1e8};
  153.         if(good(mid, cur_rng, n, x, t)) {
  154.             rng = cur_rng;
  155.             R = mid;
  156.             res = (cur_rng.F + cur_rng.S) / 2.0;
  157.         }
  158.        
  159.         else L = mid;
  160.     }
  161.    
  162.     cout << fixed << setprecision(6) << res << "\n";
  163. }
  164.  
  165. int main()
  166. {
  167.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  168.  
  169.     // #ifndef ONLINE_JUDGE
  170.     //     freopen("input.txt", "r", stdin);
  171.     //     freopen("output.txt", "w", stdout);
  172.     // #endif
  173.    
  174.     // #ifndef ONLINE_JUDGE
  175.     //      freopen("error.txt", "w", stderr);
  176.     // #endif
  177.    
  178.     int t = 1;
  179.     // int test = 1;
  180.     cin >> t;
  181.     while(t--) {
  182.         // cout << "Case #" << test++ << ": ";
  183.         solve();
  184.     }
  185.  
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement