Advertisement
Guest User

house near roads

a guest
Jan 31st, 2011
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. const int MN = 10000 + 10;
  8. const double fi = (sqrt(5.0) - 1.0) / 2.0;
  9. const double inf = 1e9;
  10.  
  11. struct point {
  12.     double x, y;
  13.     point(double dx, double dy) {
  14.         x = dx;
  15.         y = dy;
  16.     };
  17.     point() {
  18.     };
  19. };
  20.  
  21. int n;
  22. point a[MN][2];
  23. double d[MN];
  24.  
  25. inline double kos(point a, point b, point c) {
  26.     point v1(b.x - a.x, b.y - a.y), v2(c.x - a.x, c.y - a.y);
  27.     return fabs(v1.x * v2.y - v1.y * v2.x);
  28. }
  29.  
  30. double dist(point a, point b) {
  31.     return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
  32. }
  33.  
  34. inline double f(double x, double y) {
  35.     double mx = -1e20;
  36.     point p(x, y);
  37.     for(int i = 0; i < n; i ++)
  38.         mx = max(mx, kos(p, a[i][0], a[i][1]) / d[i]);
  39.     return mx;
  40. }
  41.  
  42. int main() {
  43.     freopen("input.txt", "r", stdin);
  44.     freopen("output.txt", "w", stdout);
  45.     cin >> n;
  46.     for(int i = 0; i < n; i ++) {
  47.         cin >> a[i][0].x >> a[i][0].y >> a[i][1].x >> a[i][1].y;
  48.         d[i] = dist(a[i][0], a[i][1]);
  49.     }
  50.     double left_x = -inf;
  51.     double right_x = inf;
  52.     double x1, y1, x2, y2, rez1, rez2, l_y, left_y, right_y;
  53.     double l_x = right_x - left_x;
  54.     x1 = right_x - l_x * fi;
  55.     x2 = left_x + l_x * fi;
  56.     for(int i = 0; i < 150; i ++) {
  57.         left_y = -inf;
  58.         right_y = inf;
  59.         l_y = right_y - left_y;
  60.         y1 = right_y - l_y * fi;
  61.         y2 = left_y + l_y * fi;
  62.         rez1 = f(x1, y1);
  63.         rez2 = f(x2, y2);
  64.  
  65.         for(int j = 0; j < 150; j ++) {
  66.             if (rez1 > rez2) {
  67.                 left_y = y1;
  68.                 y1 = y2;
  69.                 rez1 = rez2;
  70.                 l_y = right_y - left_y;
  71.                 y2 = left_y + l_y * fi;
  72.                 rez2 = f(x2, y2);
  73.                
  74.             }
  75.             else {
  76.                 right_y = y2;
  77.                 y2 = y1;
  78.                 rez2 = rez1;
  79.                 l_y = right_y - left_y;
  80.                 y1 = right_y - l_y * fi;
  81.                 rez1 = f(x1, y1);
  82.             }      
  83.         }
  84.         rez1 = f(x1, (right_y + left_y) / 2.0);
  85.         rez2 = f(x2, (right_y + left_y) / 2.0);
  86.         if (rez1 > rez2) {
  87.             left_x = x1;
  88.             x1 = x2;
  89.             rez1 = rez2;
  90.             l_x = right_x - left_x;
  91.             x2 = left_x + l_x * fi;
  92.             rez2 = f(x2, y2);
  93.         }
  94.         else {
  95.             right_x = x2;
  96.             x2 = x1;
  97.             rez2 = rez1;
  98.             l_x = right_x - left_x;
  99.             x1 = right_x - l_x * fi;
  100.             rez1 = f(x1, y1);
  101.         }      
  102.     }
  103.     printf("%0.18lf %0.18lf", (right_x + left_x) / 2.0, (right_y + left_y) / 2.0);
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement