poj 2079 Triangle(旋转卡壳)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2079 Triangle(旋转卡壳)相关的知识,希望对你有一定的参考价值。
Triangle
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 8917 | Accepted: 2650 |
Description
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.
Input
The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −104 <= xi, yi <= 104 for all i = 1 . . . n.
Output
For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.
Sample Input
3 3 4 2 6 2 7 5 2 6 3 9 2 0 8 0 6 5 -1
Sample Output
0.50 27.00
Source
【思路】
凸包+旋转卡壳
旋转卡壳:先确定两个点,叉积寻找最大的第三点,然后改变第二个点继续。
Quote:求点集中的最大三角形面积,O(n) 的旋转卡壳,先凸包,然后选取开头三个点 p,q,r 开始旋转,注意 r 不超过第一个点,q 不超过 r,p 不超过 q 。每次做三次推进,先推进 r,使 pq 不动面积最大,然后推进 q,再推进 p,如果三次都没有推进过,r 推进一格。每次推进完一个点都更新一下面积最大值。
【代码】
1 #include<cstdio> 2 #include<vector> 3 #include<iostream> 4 #include<algorithm> 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 6 using namespace std; 7 8 struct Pt { 9 int x,y; 10 Pt(int x=0,int y=0):x(x),y(y) {}; 11 }; 12 typedef Pt vec; 13 14 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); } 15 bool operator < (const Pt& a,const Pt& b) { 16 return a.x<b.x || (a.x==b.x && a.y<b.y); 17 } 18 bool operator == (const Pt& a,const Pt& b) { 19 return a.x==b.x && a.y==b.y; 20 } 21 int cross(vec A,vec B) { return A.x*B.y-A.y*B.x; } 22 int dist(Pt A,Pt B) { 23 return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y); 24 } 25 vector<Pt> ConvexHull(vector<Pt> p) { 26 sort(p.begin(),p.end()); 27 p.erase(unique(p.begin(),p.end()),p.end()); 28 int n=p.size() , m=0; 29 vector<Pt> ch(n+1); 30 for(int i=0;i<n;i++) { 31 while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 32 ch[m++]=p[i]; 33 } 34 int k=m; 35 for(int i=n-2;i>=0;i--) { 36 while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 37 ch[m++]=p[i]; 38 } 39 if(n>1) m--; 40 ch.resize(m); return ch; 41 } 42 43 int n; 44 vector<Pt> p,ch; 45 46 int RC() { 47 int n=ch.size(); 48 int ans=0 , cur=1 , j,k; 49 Pt v; 50 FOR(i,0,n-1) { 51 j=(i+1)%n , k=(j+1)%n; 52 while(j!=i && k!=i) { 53 ans=max(ans,abs(cross(ch[j]-ch[i],ch[k]-ch[i]))); 54 while(cross(ch[i]-ch[j],ch[(k+1)%n]-ch[k])<0) k=(k+1)%n; 55 j=(j+1)%n; 56 } 57 } 58 return ans; 59 } 60 61 int main() { 62 freopen("in.in","r",stdin); 63 freopen("out.out","w",stdout); 64 while(scanf("%d",&n)==1 && n!=-1) { 65 p.clear() , ch.clear(); 66 int x,y; 67 FOR(i,1,n) { 68 scanf("%d%d",&x,&y); 69 p.push_back(Pt(x,y)); 70 } 71 ch=ConvexHull(p); 72 printf("%.2lf\n",RC()/2.0); 73 } 74 return 0; 75 }
以上是关于poj 2079 Triangle(旋转卡壳)的主要内容,如果未能解决你的问题,请参考以下文章