Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <set>
  7. #include <vector>
  8. #include <memory.h>
  9. #include <cstdio>
  10. #include <cmath>
  11. #include <map>
  12. #include <queue>
  13. #include <unordered_map>
  14. using namespace std;
  15. typedef long long ll;
  16. const int mod = 1e9 + 9;
  17. const ll inf = 4e18;
  18. const int N = 200005;
  19. struct pt {
  20.      double x, y;
  21. };
  22.  
  23. bool cmp (pt a, pt b) {
  24.     return a.x < b.x || a.x == b.x && a.y < b.y;
  25. }
  26.  
  27. bool cw (pt a, pt b, pt c) {
  28.     return a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y) < 0;
  29. }
  30.  
  31. bool ccw (pt a, pt b, pt c) {
  32.     return a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y) > 0;
  33. }
  34.  
  35. void convex_hull (vector<pt> & a) {
  36.     if (a.size() == 1)  return;
  37.     sort (a.begin(), a.end(), &cmp);
  38.     pt p1 = a[0],  p2 = a.back();
  39.     vector<pt> up, down;
  40.     up.push_back (p1);
  41.     down.push_back (p1);
  42.     for (size_t i=1; i<a.size(); ++i) {
  43.         if (i==a.size()-1 || cw (p1, a[i], p2)) {
  44.             while (up.size()>=2 && !cw (up[up.size()-2], up[up.size()-1], a[i]))
  45.                 up.pop_back();
  46.             up.push_back (a[i]);
  47.         }
  48.         if (i==a.size()-1 || ccw (p1, a[i], p2)) {
  49.             while (down.size()>=2 && !ccw (down[down.size()-2], down[down.size()-1], a[i]))
  50.                 down.pop_back();
  51.             down.push_back (a[i]);
  52.         }
  53.     }
  54.     a.clear();
  55.     for (size_t i=0; i<up.size(); ++i)
  56.         a.push_back (up[i]);
  57.     for (size_t i=down.size()-2; i>0; --i)
  58.         a.push_back (down[i]);
  59. }
  60. vector <pt> hull;
  61. int n;
  62. double dist_v(double a, double b, double c, int i){
  63.     double numerator = a * hull[i].x + b * hull[i].y + c;
  64.     double denominator = sqrt(a * a + b * b);
  65.     return fabs(numerator / denominator);
  66. }
  67. double dist_h(double x, double y, int i){
  68.     pt a, b;
  69.     b.x = x;
  70.     b.y = y;
  71.     a.x = hull[i].x;
  72.     a.y = hull[i].y;
  73.     double projection = (a.x * b.x + a.y * b.y) / sqrt(b.x * b.x + b.y * b.y);
  74.     return projection;
  75. }
  76. int main(){
  77.     cin >> n;
  78.     for(int i = 0; i < n; ++i){
  79.         double x, y;
  80.         cin >> x >> y;
  81.         hull.push_back({x, y});
  82.     }
  83.     convex_hull(hull);
  84.     int n = (int)hull.size();
  85.     for(int i = 0; i < n; ++i){
  86.         hull.push_back(hull[i]);
  87.     }
  88.     double main_ans = inf;
  89.     int l, r;
  90.     for(int i = 0; i < n; ++i){
  91.         double x = hull[i + 1].x - hull[i].x;
  92.         double y = hull[i + 1].y - hull[i].y;
  93.         double a = hull[i].y - hull[i + 1].y;
  94.         double b = hull[i + 1].x - hull[i].x;
  95.         double c = -a * hull[i].x - b * hull[i].y;
  96.         l = i + 1;
  97.         r = i + n;
  98.         while(r - l >= 3){
  99.             double m1 = l + (r - l) / 3,
  100.             m2 = r - (r - l) / 3;
  101.             if (dist_v(a, b, c, m1) < dist_v(a, b, c, m2))
  102.                 l = m1;
  103.             else
  104.                 r = m2;
  105.         }
  106.         double dist_vertical = dist_v(a, b, c, l);
  107.         for(int it = l + 1; it <= r; ++it){
  108.             dist_vertical = max(dist_vertical, dist_v(a, b, c, it));
  109.         }
  110.         l = i + 1;
  111.         r = i + n;
  112.         while(r - l >= 3){
  113.             double m1 = l + (r - l) / 3,
  114.             m2 = r - (r - l) / 3;
  115.             if (dist_h(x, y , m1) < dist_h(x, y, m2))
  116.                 l = m1;
  117.             else
  118.                 r = m2;
  119.         }
  120.         double mx = dist_h(x, y, l);
  121.         for(int it = l + 1; it <= r; ++it){
  122.             mx = max(mx, dist_h(x, y, it));
  123.         }
  124.         l = i + 1;
  125.         r = i + n;
  126.         while(r - l >= 3){
  127.             double m1 = l + (r - l) / 3,
  128.             m2 = r - (r - l) / 3;
  129.             if (dist_h(x, y , m1) > dist_h(x, y, m2))
  130.                 l = m1;
  131.             else
  132.                 r = m2;
  133.         }
  134.         double mn = dist_h(x, y, l);
  135.         for(int it = l + 1; it <= r; ++it){
  136.             mn = min(mn, dist_h(x, y, it));
  137.         }
  138.         double dist_horizontal = mx - mn;
  139.         main_ans = min(main_ans, dist_horizontal * dist_vertical);
  140.     }
  141.     cout << (ll)(main_ans + 0.5) << endl;
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement