首页 > 代码库 > POJ 2079 Triangle [旋转卡壳]
POJ 2079 Triangle [旋转卡壳]
Triangle
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 9525 | Accepted: 2845 |
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
33 42 62 752 63 92 08 06 5-1
Sample Output
0.5027.00
Source
Shanghai 2004 Preliminary
选三个点三角形面积最大
这三个点一定在凸包上
可以O(n),猜i,j,k单调,然后和旋转卡壳一样枚举i,先让k跑,再让j跑
事实证明貌似真的单调,discuss里的数据并不能卡掉我的程序....
注意:跑的时候用面积判断是不是跑到下一个
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>using namespace std;typedef long long ll;const int N=5e4+5;const double eps=1e-8;inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f;}inline int sgn(double x){ if(abs(x)<eps) return 0; else return x<0?-1:1;}struct Vector{ double x,y; Vector(double a=0,double b=0):x(a),y(b){} bool operator <(const Vector &a)const{ return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0); }};typedef Vector Point;Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}double Len(Vector a){return sqrt(Dot(a,a));}double Len2(Vector a){return Dot(a,a);}double DisTL(Point p,Point a,Point b){ Vector v1=p-a,v2=b-a; return abs(Cross(v1,v2)/Len(v2));}int ConvexHull(Point p[],int n,Point ch[]){ sort(p+1,p+1+n); int m=0; for(int i=1;i<=n;i++){ while(m>1&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--; ch[++m]=p[i]; } int k=m; for(int i=n-1;i>=1;i--){ while(m>k&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--; ch[++m]=p[i]; } if(n>1) m--; return m;}double RotatingCalipers(Point p[],int n){ if(n<=2) return 0; if(n==3) return abs(Cross(p[3]-p[1],p[2]-p[1])); int j=2,k=3; double ans=0; p[n+1]=p[1]; for(int i=1;i<=n;i++){ while(sgn(DisTL(p[k],p[i],p[j])-DisTL(p[k+1],p[i],p[j]))<=0) k=k%n+1; //while(sgn(abs(Cross(p[k]-p[i],p[k]-p[j]))-abs(Cross(p[k+1]-p[i],p[k+1]-p[j])))<=0) k=k%n+1; ans=max(ans,abs(Cross(p[k]-p[i],p[k]-p[j]))); //while(sgn(DisTL(p[k],p[i],p[j])-DisTL(p[k],p[i],p[j+1]))<=0) j=j%n+1; while(abs(Cross(p[k]-p[i],p[k]-p[j]))-abs(Cross(p[k]-p[i],p[k]-p[j+1]))<=0) j=j%n+1; ans=max(ans,abs(Cross(p[k]-p[i],p[k]-p[j]))); } return ans;}int n;Point p[N],ch[N];int main(int argc, const char * argv[]) { while(true){ n=read();if(n==-1) break; for(int i=1;i<=n;i++) p[i].x=read(),p[i].y=read(); n=ConvexHull(p,n,ch); double ans=RotatingCalipers(ch,n); printf("%.2f\n",ans/2); }}
POJ 2079 Triangle [旋转卡壳]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。