fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<pair<int,int>> a;
  25.  
  26. bool isPossible(int n, double mid){
  27.  
  28. double x = -1, y = -1;
  29. for(int i=0; i<n; i++){
  30. double pos = a[i].first;
  31. double v = a[i].second;
  32.  
  33. double l = pos-v*mid;
  34. double r = pos+v*mid;
  35.  
  36. if(x == -1 && y == -1){
  37. x = l;
  38. y = r;
  39. }
  40. else{
  41. if(y < l ) return false;
  42. y = min(y, r);
  43. x = max(x, l);
  44. }
  45. }
  46. return true;
  47. }
  48.  
  49. double consistency1(int n){
  50.  
  51. sort(begin(a), end(a));
  52.  
  53. double precision = 1e-7;
  54.  
  55. double s = 0, e = INF;
  56.  
  57. int iterations = 60; // 100 safer
  58.  
  59. for(int i=0; i<iterations; i++){
  60. double mid = s + (e-s)/2;
  61. if(isPossible(n, mid)){
  62. e = mid;
  63. }
  64. else s = mid;
  65. }
  66.  
  67. return e;
  68.  
  69. }
  70.  
  71.  
  72.  
  73. double consistency2(int n){
  74.  
  75. sort(begin(a), end(a));
  76.  
  77. double precision = 1e-7;
  78.  
  79. double s = 0, e = INF;
  80. double ans = INF;
  81.  
  82. int iterations = 60; // 100 safer
  83.  
  84. for(int i=0; i<iterations; i++){
  85. double mid = s + (e-s)/2;
  86. if(isPossible(n, mid)){
  87. ans = min(ans, mid);
  88. e = mid+precision;
  89. }
  90. else s = mid-precision;
  91. }
  92.  
  93. return ans;
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. void solve() {
  106.  
  107. int n;
  108. cin>>n;
  109.  
  110. a.resize(n);
  111. for(int i=0; i<n; i++){
  112. int x , v;
  113. cin >> x >> v;
  114. a[i] = {x, v};
  115. }
  116.  
  117. cout << fixed << setprecision(7) << consistency1(n) << endl;
  118. // cout << fixed << setprecision(7) << consistency1(n) << " " << consistency2(n) << endl;
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. int32_t main() {
  128. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  129.  
  130. int t = 1;
  131. while (t--) {
  132. solve();
  133. }
  134.  
  135. return 0;
  136. }
Success #stdin #stdout 0s 5320KB
stdin
5
-1 5
10 3
4 2
7 10
8 1
stdout
1.5000000