abinash_hstu

Sana In The Forest

Jan 10th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. //OM
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cctype>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <vector>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. #include <list>
  13. #include <stack>
  14. #include <queue>
  15. #include <utility>
  16. #include <sstream>
  17. #include <algorithm>
  18. using  namespace  std;
  19.  
  20. #define  x first
  21. #define  y second
  22. #define  pb push_back
  23. #define  mp make_pair
  24. #define  PI (acos(-1.0))
  25. #define  mem(a,b) memset(a,b,sizeof(a))
  26. #define  Sort(x) sort(x.begin(),x.end())
  27. #define  FOR(i, b, e) for(int i = b; i <= e; i++)
  28. #define  FORR(i, b, e) for(int i = b; i >= e; i--)
  29. #define  FORI(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  30. #define  pr(x) cout<<x<<"\n"
  31. #define  prs(x) cout<<x<<" "
  32. #define  pr2(x,y) cout<<x<<" "<<y<<"\n"
  33. #define  pr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
  34. #define  ppr(a) cout<<a.x<<" "<<a.y<<"\n"
  35.  
  36. typedef  long long ll;
  37. typedef  pair <ll, ll> pii;
  38. typedef  pair <double , double> pdd;
  39. typedef  vector <int> vi;
  40. typedef  vector <pii> vpii;
  41.  
  42. //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
  43. //int dx[]={1,1,0,-1,-1,-1,0,1};
  44. //int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
  45.  
  46. #define  EPS 1e-9
  47. #define  MAX 1000007
  48. #define READ(f) freopen(f, "r", stdin)
  49. #define WRITE(f) freopen(f, "w", stdout)
  50.  
  51. /*
  52. Problem: Sana in the forest
  53. Level: Easy Medium
  54. Author: Abinash Ghosh
  55. Algorithm/Technique: Convex Hull
  56. Complexity: O(n log n)
  57. Analysis:
  58.     Basic Computational geometry problem. For minimize the length of fence , you have to construct
  59.     a convex polygon covering all the trees and perimeter of the polyon is the required value.
  60. */
  61.  
  62. pair<int , int > p0,pnt[MAX],res[MAX];
  63. inline ll cross(pii a, pii b, pii c)
  64. {
  65.     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
  66. }
  67. void Convex_Hull(int np,int &nc)
  68. {
  69.     sort(pnt,pnt+np);
  70.     int j=0;
  71.     //use <= to remove colinear points
  72.     FOR(i,0,np-1){
  73.         while(j>=2&&cross(res[j-2],res[j-1],pnt[i])<=0)j--;
  74.         res[j++]=pnt[i];
  75.     }
  76.     for(int i=np-2,t=j+1; i>=0; i--){
  77.         while(j>=t&&cross(res[j-2],res[j-1],pnt[i])<=0)j--;
  78.         res[j++]=pnt[i];
  79.     }
  80.     nc=j-1;
  81. }
  82. double pPerimeter(int n)
  83. {
  84.     double result=0,dx,dy;
  85.     for(int i=0;i<n;++i)
  86.     {
  87.         dx= res[(i+1)%n].x - res[i].x;
  88.         dy= res[(i+1)%n].y - res[i].y;
  89.         result+=sqrt(dx*dx+dy*dy);
  90.     }
  91.     return result;
  92. }
  93. int main()
  94. {
  95.     //READ("in.txt");
  96.     //WRITE("out.txt");
  97.     int T;
  98.     scanf("%d",&T);
  99.     FOR(cs,1,T)
  100.     {
  101.         int point,Bpoint;
  102.         scanf("%d",&point);
  103.         FOR(i,0,point-1)
  104.         scanf("%d %d",&pnt[i].x,&pnt[i].y);
  105.         Convex_Hull(point,Bpoint);
  106.         double ans=pPerimeter(Bpoint);
  107.         printf("%.6lf\n",ans);
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment