Advertisement
Guest User

багифичисливмуниципалки

a guest
Dec 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define all(x) x.begin(), x.end()
  6. #define endl '\n'
  7. #define sz(x) (int)(x).size()
  8. #define mp(x, y) make_pair(x, y)
  9. #define pb push_back
  10. #define pii pair<int, int>
  11. #define rep(i, f, t) for (auto i = (f); i < (t); ++i)
  12. #define ui unsigned int
  13. #define ll long long
  14. #define double long double
  15. #define ull unsigned long long
  16.  
  17. const int inf = (int)(2e9);
  18. const ll INF = (ll)(1e18);
  19. const int MOD = (int)(1e9 + 7);
  20. const double eps = (double)(1e-9);
  21. const double pi = acos(-1);
  22.  
  23. const int maxn = (int)(1e5 + 10);
  24. const int maxm = (int)(2e5 + 10);
  25.  
  26. void solve();
  27.  
  28. template <typename A> inline void print(A x) { cout << x << endl; }
  29. template <typename A, typename B> inline void print(A x, B y) { cout << x << ' ' << y << endl; }
  30. template <typename A, typename B, typename C> inline void print(A x, B y, C z) { cout << x << ' ' << y << ' ' << z << endl; }
  31. template <typename A> inline void in(A &x) { cin >> x; }
  32. template <typename A, typename B> inline void in(A &x, B &y) { cin >> x >> y; }
  33. template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { cin >> x >> y >> z; }
  34. template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &p) { cin >> x >> y >> z >> p; }
  35. template <typename A> inline void read(A begin, A end) { while (begin != end) cin >> *(begin++); }
  36. template <typename A> inline void write(A begin, A end) { while (begin != end) { cout << *(begin++) << ' '; } cout << endl; }
  37.  
  38.  
  39. signed main()
  40. {
  41.     std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  42.     srand((ui)(time(0)));
  43.     cout << fixed << setprecision(20);
  44.     solve();
  45.     return 0;
  46. }
  47.  
  48.  
  49. struct Point {
  50.     double x;
  51.     double y;
  52.     Point() {}
  53.     Point(const double& _x, const double& _y) {
  54.         x = _x;
  55.         y = _y;
  56.     }
  57. };
  58. bool operator==(const Point& a, const Point& b) {
  59.     return a.x == b.x && a.y == b.y;
  60. }
  61. double polar_angle(const Point& p) {
  62.     double alpha = atan2(p.y, p.x);
  63.     if (alpha < 0) alpha += 2 * pi;
  64.     return alpha;
  65. }
  66. istream& operator>>(istream& in, Point& p)
  67. {
  68.     in >> p.x >> p.y;
  69.     return in;
  70. }
  71. ostream& operator<<(ostream& out, Point& p) {
  72.     out << p.x << ' ' << p.y;
  73.     return out;
  74. }
  75.  
  76.  
  77.  
  78. struct Vector {
  79.     double x;
  80.     double y;
  81.     Vector() {}
  82.     Vector(const double& _x, const double& _y) {
  83.         x = _x;
  84.         y = _y;
  85.     }
  86.     Vector(const Point& a, const Point& b) {
  87.         x = b.x - a.x;
  88.         y = b.y - a.y;
  89.     }
  90. };
  91. double len(const Vector& a) {
  92.     return sqrt(a.x * a.x + a.y * a.y);
  93. }
  94. double scalar_product(const Vector& a, const Vector& b)
  95. {
  96.     return a.x * b.x + a.y * b.y;
  97. }
  98. double cross_product(const Vector& a, const Vector& b)
  99. {
  100.     return a.x * b.y - b.x * a.y;
  101. }
  102. double alpha(const Vector& a, const Vector& b) {
  103.     return acos(scalar_product(a, b) / (len(a) * len(b)));
  104. }
  105. Vector norm(const Vector& a) {
  106.     Vector res;
  107.     double l = len(a);
  108.     res.x = a.x / l;
  109.     res.y = a.y / l;
  110.     return res;
  111. }
  112.  
  113.  
  114.  
  115. struct Line {
  116.     double a;
  117.     double b;
  118.     double c;
  119.     Line() {}
  120.     Line(const double& _A, const double& _B, const double& _C) {
  121.         a = _A;
  122.         b = _B;
  123.         c = _C;
  124.     }
  125.     Line(const Point& first_point, const Point& second_point) {
  126.         a = second_point.y - first_point.y;
  127.         b = first_point.x - second_point.x;
  128.         c = second_point.x * first_point.y - first_point.x * second_point.y;
  129.     }
  130. };
  131. istream& operator>>(istream& in, Line& line) {
  132.     in >> line.a >> line.b >> line.c;
  133.     return in;
  134. }
  135. bool point_in_line(const Point& p, const Line& line) {
  136.     return abs(line.a * p.x + line.b * p.y + line.c) < eps;
  137. }
  138. double dist_to_line_unsigned(const Point& p, const Line& line) {
  139.     return abs(line.a * p.x + line.b * p.y + line.c) / sqrt(line.a * line.a + line.b * line.b);
  140. }
  141. double dist_to_line_signed(const Point& p, const Line& line) {
  142.     return -(line.a * p.x + line.b * p.y + line.c) / sqrt(line.a * line.a + line.b * line.b);
  143. }
  144. Point lines_intersection(const Line& first_line, const Line& second_line) {
  145.     Point result;
  146.     double matrix_determinant = (first_line.a * second_line.b - second_line.a * first_line.b);
  147.     result.x = (first_line.b * second_line.c - second_line.b * first_line.c) / matrix_determinant;
  148.     result.y = (first_line.c * second_line.a - second_line.c * first_line.a) / matrix_determinant;
  149.     return result;
  150. }
  151.  
  152.  
  153.  
  154. struct Segment {
  155.     Point a;
  156.     Point b;
  157.     Segment() {}
  158.     Segment(const Point& _a, const Point& _b) {
  159.         a = _a;
  160.         b = _b;
  161.     }
  162. };
  163. istream& operator>>(istream& in, Segment& s) {
  164.     in >> s.a >> s.b;
  165.     return in;
  166. }
  167. double dist_to_segment(const Point& p, const Segment& seg) {
  168.     Vector ab(seg.a, seg.b), ac(seg.a, p), bc(seg.b, p), ba(seg.b, seg.a);
  169.     double result = min(len(ac), len(bc));
  170.     Line line(seg.a, seg.b);
  171.     if ((pi / 2.0) - alpha(ab, ac) > eps && (pi / 2.0) - alpha(bc, ba) > eps)
  172.         result = min(result, dist_to_line_signed(p, line));
  173.     return result;
  174. }
  175. bool point_in_segment(const Point& p, const Segment& seg) {
  176.     double length = len(Vector(seg.a, seg.b));
  177.     double len1 = len(Vector(seg.a, p)), len2 = len(Vector(seg.b, p));
  178.     return (abs(length - (len1 + len2)) < eps);
  179. }
  180.  
  181.  
  182. Point a, b, c, d;
  183. Segment ab, cd;
  184. double v1_x, v2_x, v1_y, v2_y;
  185.  
  186. double f(double t)
  187. {
  188.     Point qa(a.x + v1_x * t, a.y + v1_y * t), qb(b.x + v1_x * t, b.y + v1_y * t), qc(c.x + v2_x * t, c.y + v2_y * t), qd(d.x + v2_x * t, d.y + v2_y * t);
  189.     ab = Segment(qa, qb);
  190.     cd = Segment(qc, qd);
  191.     double ans = dist_to_segment(qa, cd);
  192.     if (ans < 0) ans = 1000000.0;
  193.     ans = min(ans, dist_to_segment(qb, cd));
  194.     if (ans < 0) ans = 1000000.0;
  195.     ans = min(ans, dist_to_segment(qc, ab));
  196.     if (ans < 0) ans = 1000000.0;
  197.     ans = min(ans, dist_to_segment(qd, ab));
  198.     if (ans < 0) ans = 1000000.0;
  199.     return ans;
  200. }
  201.  
  202. double ternary_search()
  203. {
  204.     double l = 0.0, r = 1e5;
  205.     while (r - l > eps) {
  206.         double m1 = l + (r - l) / 3;
  207.         double m2 = r - (r - l) / 3;
  208.         cout << f(m1) << ' ' << f(m2) << endl;
  209.         if (f(m1) > f(m2))
  210.             l = m1;
  211.         else
  212.             r = m2;
  213.     }
  214.     cout << f(l) << endl;
  215.     return l;
  216. }
  217.  
  218. void solve()
  219. {
  220.     cin >> a >> b >> c >> d;
  221.     cin >> v1_x >> v1_y >> v2_x >> v2_y;
  222.     cout << ternary_search() << endl;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement