1. 程式人生 > >[hdu3934] 凸包 旋轉卡殼

[hdu3934] 凸包 旋轉卡殼

space false mem isp display printf oss style stack

大致題意:

  求多邊形的最大內接三角形

  

  旋轉卡殼 模板題

技術分享圖片
  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<set>
  7 #include<map>
  8 #include<stack>
  9 #include<time.h>
 10 #include<cstdlib>
 11
#include<cmath> 12 #include<list> 13 using namespace std; 14 #define MAXN 100100 15 #define eps 1e-9 16 #define For(i,a,b) for(int i=a;i<=b;i++) 17 #define Fore(i,a,b) for(int i=a;i>=b;i--) 18 #define lson l,mid,rt<<1 19 #define rson mid+1,r,rt<<1|1 20 #define mkp make_pair 21
#define pb push_back 22 #define cr clear() 23 #define sz size() 24 #define met(a,b) memset(a,b,sizeof(a)) 25 #define iossy ios::sync_with_stdio(false) 26 #define fre freopen 27 #define pi acos(-1.0) 28 #define inf 1e6+7 29 #define Vector Point 30 const int Mod=1e9+7; 31 typedef unsigned long
long ull; 32 typedef long long ll; 33 int dcmp(double x){ 34 if(fabs(x)<=eps) return 0; 35 return x<0?-1:1; 36 } 37 struct Point{ 38 double x,y; 39 Point(double x=0,double y=0):x(x),y(y) {} 40 bool operator < (const Point &a)const{ 41 if(x==a.x) return y<a.y; 42 return x<a.x; 43 } 44 Point operator - (const Point &a)const{ 45 return Point(x-a.x,y-a.y); 46 } 47 Point operator + (const Point &a)const{ 48 return Point(x+a.x,y+a.y); 49 } 50 Point operator * (const double &a)const{ 51 return Point(x*a,y*a); 52 } 53 Point operator / (const double &a)const{ 54 return Point(x/a,y/a); 55 } 56 void read(){ 57 scanf("%lf%lf",&x,&y); 58 } 59 void out(){ 60 cout<<"debug: "<<x<<" "<<y<<endl; 61 } 62 bool operator == (const Point &a)const{ 63 return dcmp(x-a.x)==0 && dcmp(y-a.y)==0; 64 } 65 }; 66 double Dot(Vector a,Vector b) { 67 return a.x*b.x+a.y*b.y; 68 } 69 double dis(Vector a) { 70 return sqrt(Dot(a,a)); 71 } 72 double Cross(Point a,Point b){ 73 return a.x*b.y-a.y*b.x; 74 } 75 int ConvexHull(Point *p,int n,Point *ch){ 76 int m=0; 77 For(i,0,n-1) { 78 while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 79 ch[m++]=p[i]; 80 } 81 int k=m; 82 Fore(i,n-2,0){ 83 while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 84 ch[m++]=p[i]; 85 } 86 if(n>1) m--; 87 return m; 88 } 89 void Swap(int &p1,int &p2){ 90 p1^=p2; 91 p2^=p1; 92 p1^=p2; 93 } 94 double getAns(Point *ch,int n) { 95 double ans=0; 96 For(i,0,n-1){ 97 int j=(i+1)%n; 98 int k=(j+1)%n; 99 while(j!=k && k!=i){ 100 while(Cross(ch[j]-ch[i],ch[(k+1)%n]-ch[i])>=Cross(ch[j]-ch[i],ch[k]-ch[i])) k=(k+1)%n; 101 ans=max(ans,Cross(ch[j]-ch[i],ch[k]-ch[i])); 102 j=(j+1)%n; 103 } 104 } 105 return ans; 106 } 107 int n,m; 108 Point p[1000005]; 109 Point ch[1000005]; 110 void solve(){ 111 For(i,0,n-1) p[i].read(); 112 sort(p,p+n); 113 int m=ConvexHull(p,n,ch); 114 printf("%.2lf\n",getAns(ch,m)/2); 115 } 116 int main(){ 117 // fre("in.txt","r",stdin); 118 int t=0; 119 while(~scanf("%d",&n)) solve(); 120 return 0; 121 }
View Code

[hdu3934] 凸包 旋轉卡殼