Advertisement
zholnin

UVA 10135 - non-working

May 30th, 2016
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <vector>
  4. #include <list>
  5. #include <map>
  6. #include <set>
  7. #include <deque>
  8. #include <stack>
  9. #include <bitset>
  10. #include <algorithm>
  11. #include <functional>
  12. #include <numeric>
  13. #include <utility>
  14. #include <sstream>
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <cstdio>
  18. #include <cmath>
  19. #include <cstdlib>
  20. #include <ctime>
  21. #include <iostream>
  22. #include <queue>
  23. #include <assert.h>
  24.  
  25. using namespace std;
  26.  
  27.  
  28. struct point
  29. {
  30.     double x = 0;
  31.     double y = 0;
  32.  
  33.     double distance() const;
  34.     double angle() const;
  35. };
  36.  
  37. point center;
  38.  
  39. double point::distance() const { return (x - center.x)*(x - center.x) + (y - center.y)*(y - center.y); };
  40. double point::angle() const { return atan2(x - center.x, y - center.y); };
  41.  
  42.  
  43. bool operator<(const point&A, const point& B)
  44. {
  45.     if (A.angle() + 1e-10 < B.angle()) return true;
  46.     if (A.angle() - 1e-10 > B.angle()) return false;
  47.    
  48.     return A.distance() < B.distance();
  49. }
  50.  
  51. bool operator==(const point&A, const point& B)
  52. {
  53.     return A.x == B.x && A.y == B.y;
  54. }
  55.  
  56.  
  57. bool cw(point& a, point& b, point& c) {
  58.     return a.x*(b.y - c.y) + b.x*(c.y - a.y) + c.x*(a.y - b.y) < 1e-10;
  59. }
  60.  
  61. double convex_hull(vector<point> &D, int s, vector<point> &hull)
  62. {
  63.     for (size_t j = 0; j < D.size(); j++)
  64.     {
  65.         int i = (j + s) % D.size();
  66.  
  67.         while (hull.size() > 1 && !cw(hull[hull.size() - 2], hull[hull.size() - 1], D[i]))
  68.             hull.pop_back();
  69.  
  70.         hull.push_back(D[i]);
  71.     }
  72.  
  73.     double sum = 0.0;
  74.  
  75.     for (size_t i = 0; i < hull.size(); i++)
  76.     {
  77.         point &v1 = hull[i];
  78.  
  79. //      cout << fixed << setprecision(2) << v1.x << " " << v1.y << "\n";
  80.  
  81.         point &v2 = hull[(i + 1) % hull.size()];
  82.         double distance = sqrt((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
  83.         sum += distance;
  84.     }
  85.  
  86. //  cout << sum + 2.0 << "\n\n";
  87.     return sum;
  88. }
  89.  
  90. int main()
  91. {
  92.     ios_base::sync_with_stdio(0);
  93.  
  94.     //freopen("test.txt", "r", stdin);
  95.     //freopen("reagents.out", "w", stdout);
  96.  
  97.     int t;
  98.     cin >> t;
  99.  
  100.     while (t--)
  101.     {
  102.         center.x = 0;
  103.         center.y = 0;
  104.  
  105.         int n;
  106.         cin >> n;
  107.        
  108.         vector<point> D1(n);
  109.  
  110.         for (int i = 0; i < n; i++)
  111.         {
  112.             cin >> D1[i].x >> D1[i].y;
  113.             center.x += D1[i].x;
  114.             center.y += D1[i].y;
  115.         }
  116.  
  117.         center.x /= n + 1;
  118.         center.y /= n + 1;
  119.  
  120.         vector<point> D(D1);
  121.         point C;
  122.         D.push_back(C);
  123.  
  124.         sort(D.begin(), D.end());
  125.         int best = 0;
  126.         for (size_t i = 0; i < D.size(); i++)
  127.         if (D[best].distance() < D[i].distance()) best = i;
  128.  
  129.         vector<point> hull;
  130.         hull.push_back(D[best]);
  131.         double best_length = convex_hull(D, best + 1, hull);
  132.  
  133.         if (find(hull.begin(), hull.end(), C) == hull.end())
  134.         {
  135.             best_length = 1e30;
  136.  
  137.             center.x = 0;
  138.             center.y = 0;
  139.  
  140.             sort(D1.begin(), D1.end());
  141.  
  142.             for (size_t i = 0; i < D.size(); i++)
  143.             {
  144.                 hull.clear();
  145.                 hull.push_back(C);
  146.  
  147.                 best_length = min(best_length, convex_hull(D1, i, hull));
  148.             }
  149.         }
  150.  
  151.         cout << fixed << setprecision(2) << best_length + 2.0 <<  "\n";
  152.         if (t != 0) cout << "\n";
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement