Advertisement
Guest User

race time - ternary search

a guest
Jul 7th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 100005;
  4. int speed[MAXN], dist[MAXN];
  5. int main()
  6. {
  7. // freopen("input.txt", "r", stdin);
  8. ios_base::sync_with_stdio(0);
  9. int n,k;
  10. cin>>n>>k;
  11. for (int i = 0; i < n; ++i)
  12. {
  13. cin>>speed[i]>>dist[i];
  14. }
  15. long double lo = 0.0, hi = k;
  16. for (int i = 0; i < 200; ++i)
  17. {
  18. long double m1 = lo+((hi-lo)/3.0);
  19. long double m2 = hi-((hi-lo)/3.0);
  20. long double min1 = 1e20, max1 = -1e20, min2 = 1e20, max2 = -1e20;
  21. for (int i = 0; i < n; ++i)
  22. {
  23. long double val = (speed[i]*m1) + dist[i];
  24. min1 = min(min1, val);
  25. max1 = max(max1, val);
  26. val = (speed[i]*m2) + dist[i];
  27. min2 = min(min2, val);
  28. max2 = max(max2, val);
  29. }
  30. max1-=min1;
  31. max2-=min2;
  32. if(max1 < max2)
  33. hi = m2;
  34. else
  35. lo = m1;
  36. }
  37. long double min1 = 1e20, max1 = -1e20;
  38. for (int i = 0; i < n; ++i)
  39. {
  40. long double val = (speed[i]*lo) + dist[i];
  41. min1 = min(min1, val);
  42. max1 = max(max1, val);
  43. }
  44. double ans = max1-min1;
  45. cout.precision(6);
  46. cout<<fixed<<ans<<"\n";
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement