Tarango

Guarding Bananas

Aug 4th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. //==================================================================//
  2. // Name        : flash7even                                         //
  3. // Author      : Tarango Khan                                       //
  4. // Codeforces  : flash_7                                            //
  5. // Topcoder    : flash_7                                            //
  6. // Hackerrank  : flash_7                                            //
  7. // Email       : [email protected]                            //
  8. // Facebook    : flash7even                                         //
  9. //==================================================================//
  10.  
  11. //==================================================================//
  12. #include <bits/stdc++.h>                                            //
  13. using namespace std;                                                //
  14. #define read(nm)        freopen(nm, "r", stdin)                     //
  15. #define write(nm)       freopen(nm, "w", stdout)                    //
  16. #define pb              push_back                                   //
  17. #define MP              make_pair                                   //
  18. #define ff              first                                       //
  19. #define ss              second                                      //
  20. #define FABS(x)         ((x)+eps<0?-(x):(x))                        //
  21. #define POPCOUNT        __builtin_popcountll                        //
  22. #define RIGHTMOST       __builtin_ctzll                             //
  23. #define LEFTMOST(x)     (63-__builtin_clzll((x)))                   //
  24. #define NUMDIGIT(x,y)   (((int)(log10((x))/log10((y))))+1)          //
  25. #define SQ(x)           ((x)*(x))                                   //
  26. #define pf              printf                                      //
  27. #define sf              scanf                                       //
  28. #define phl             printf("hello\n")                           //
  29. #define SZ(x)           ((int)(x).size())                           //
  30. #define mems(x,v)       memset(x,v,sizeof(x))                       //
  31. #define CLR(x,y)        memset(x,y,sizeof(x))                       //
  32. #define ALL(x)          (x).begin(),(x).end()                       //
  33. #define NORM(x)         if(x>=mod)x-=mod;                           //
  34. #define MOD(x,y)        (((x)*(y))%mod)                             //
  35. #define fills(v,n)      fill(v.begin(), v.end(), n)                 //
  36. #define LL              long long                                   //
  37. #define LLU             long long unsigned int                      //
  38. #define vlong           long long                                   //
  39. #define uvlong          unsigned long long                          //
  40. #define debug1(v1)      cout<<"1@ Debug Val 1 = "<<v1<<endl;        //
  41. #define debug2(v2)      cout<<"   2@ Debug Num 2 = "<<v2<<endl;     //
  42. #define debug3(v3)      cout<<"      3@ Debug Res 3 = "<<v3<<endl;  //
  43. #define UB(v,a)         upper_bound(v.begin(),v.end(),a)-v.begin()  //
  44. #define LB(v,a)         lower_bound(v.begin(),v.end(),a)-v.begin()  //
  45. #define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
  46. //==================================================================//
  47.  
  48. //==================================================================//
  49. void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
  50.      a.erase(unique(a.begin(), a.end()), a.end()); }                //
  51. void printDouble(double f,int p){ cout << fixed;                    //
  52.      cout << setprecision(p) << f <<endl; }                         //
  53. int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
  54. int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
  55. bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
  56. LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
  57. LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
  58. LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
  59.      res = (res*x); x = (x*x);p >>= 1;} return res;}                //
  60. //==================================================================//
  61.  
  62. //==================================================================//
  63. //int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
  64. //                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
  65. //int dir8[8][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
  66. //                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
  67. //int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
  68. //==================================================================//
  69.  
  70. #ifdef forthright48
  71.      #include <ctime>
  72.      clock_t tStart = clock();
  73.      #define debug(args...) {dbg,args; cerr<<endl;}
  74.      #define timeStamp printf("Exc Time: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC)
  75. #else
  76.      #define debug(args...)
  77.      #define timeStamp
  78. #endif
  79.  
  80. struct debugger{
  81.     template<typename T> debugger& operator , (const T& v){
  82.         cerr << v << " ";
  83.         return *this;
  84.     }
  85. }dbg;
  86.  
  87. typedef pair <LL,LL> pll;
  88. typedef vector<pll> vll;
  89. typedef vector<LL> vl;
  90.  
  91. const LL inf = 2147383647;
  92. const LL MOD = 1000000007;
  93. const double pi = 2*acos(0.0);
  94. const double eps = 1e-9;
  95. #define Size 100005
  96.  
  97. //=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//
  98.  
  99. struct Point {
  100.     LL x, y;
  101. };
  102.  
  103. Point pointList[Size];
  104. Point hullPoints[Size];
  105. stack<Point> S;
  106. int N,cnt;
  107.  
  108. LL dist(Point P, Point Q) {
  109.     return SQ(P.x - Q.x) + SQ(P.y - Q.y);
  110. }
  111.  
  112. LL orientation(Point P, Point Q, Point R) {
  113.     LL ret = (Q.y - P.y) * (R.x - Q.x) - (Q.x - P.x) * (R.y - Q.y);
  114.     if (ret > 0) return 1;
  115.     if (ret < 0) return 2;
  116.     return ret;
  117. }
  118.  
  119. bool cmp(Point X, Point Y) {
  120.     LL ret = orientation(pointList[0], X, Y);
  121.     if (ret == 0) {
  122.         LL dist1 = dist(pointList[0], X);
  123.         LL dist2 = dist(pointList[0], Y);
  124.         return (dist1 < dist2);
  125.     } else if (ret == 2){
  126.         return true;
  127.     } else {
  128.         return false;
  129.     }
  130. }
  131.  
  132. Point nextToTop() {
  133.     Point P = S.top();
  134.     S.pop();
  135.     Point res = S.top();
  136.     S.push(P);
  137.     return res;
  138. }
  139.  
  140. void convexHull(int N) {
  141.     int ymin = pointList[0].y, index = 0;
  142.     for (int i = 1; i < N; i++) {
  143.         if (pointList[i].y < ymin || (pointList[i].y == ymin &&
  144.                         pointList[i].x < pointList[index].x)) {
  145.             ymin = pointList[i].y;
  146.             index = i;
  147.         }
  148.     }
  149.     swap(pointList[0], pointList[index]);
  150.     sort(&pointList[1], &pointList[N], cmp);
  151.     S.push(pointList[0]);
  152.  
  153.     for (int i = 1; i < N; i++) {
  154.         while (S.size() > 1){
  155.             if(orientation(nextToTop(),S.top(), pointList[i]) == 2) break;
  156.             S.pop();
  157.         }
  158.         S.push(pointList[i]);
  159.     }
  160.     cnt = 0;
  161.     while (!S.empty()) {
  162.         hullPoints[cnt++] = S.top();
  163.         S.pop();
  164.     }
  165. }
  166.  
  167. double angle(Point a, Point o, Point b) {
  168.     Point t1, t2;
  169.     t1.x = a.x - o.x;
  170.     t1.y = a.y - o.y;
  171.     t2.x = b.x - o.x;
  172.     t2.y = b.y - o.y;
  173.     double theta = atan2((double)t2.y, (double)t2.x) -
  174.                    atan2((double)t1.y, (double)t1.x);
  175.     if(theta < 0) theta += (2.0 * pi); /// If theta negative, we add 2*180 degree to make positive.
  176.     return theta;
  177. }
  178.  
  179. double getMaxAngelInPolygon(){
  180.     if(cnt<3) return 0;
  181.     double res = angle(hullPoints[cnt-1], hullPoints[0], hullPoints[1]);
  182.     hullPoints[cnt] = hullPoints[0];
  183.     for(int i = 1; i < cnt; i++) {
  184.         double ang = angle(hullPoints[i-1], hullPoints[i], hullPoints[i+1]);
  185.         if(ang < res) res = ang;
  186.     }
  187.     return res;
  188. }
  189.  
  190. int main() {
  191.     int N,nCase;
  192.     sf("%d",&nCase);
  193.     for(int cs = 1;cs<=nCase;cs++){
  194.         scanf("%d", &N);
  195.         for (int i = 0; i < N; i++) {
  196.             scanf("%lld %lld", &pointList[i].x, &pointList[i].y);
  197.         }
  198.         convexHull(N);
  199.         double res = getMaxAngelInPolygon();
  200.         res = res*180.0/pi;
  201.         pf("Case %d: %.7lf\n",cs,res);
  202.     }
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment