题目链接:http://poj.org/problem?id=2079
题目大意:给出n个点,然后求出这n个点中能够围城的三角形面积的最大值的三个点,输出面积。
思路:首先易得三个点都位于凸包上。然后我们枚举凸包上的每个点i。然后j=i+1,k=j+1,旋转k点,直到得到最大的三角形,此时旋转j,k两点,维护最大的三角形的面积。枚举i是凸包上的每个点即可。
旋转的时候有个小优化,在代码中已经注释出来了:
ACCode:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define Pair pair<int,int>
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// ??
//std::ios::sync_with_stdio(false);
// register
const int MAXN=5e4+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0);
const double EPS=1.0e-8;
struct Point{
double x,y,t,d;
Point(double _x=0,double _y=0,double _t=0,double _d=0){
x=_x;y=_y;t=_t;d=_d;
}
friend Point operator + (const Point &a,const Point &b){
return Point(a.x+b.x,a.y+b.y);
}
friend Point operator - (const Point &a,const Point &b){
return Point(a.x-b.x,a.y-b.y);
}
friend double operator ^ (Point a,Point b){//???????
return a.x*b.y-a.y*b.x;
}
friend int operator == (const Point &a,const Point &b){
if(fabs(a.x-b.x)<EPS&&fabs(a.y-b.y)<EPS) return 1;
return 0;
}
friend double operator * (const Point &a,const Point &b){
return a.x*b.x+a.y*b.y;
}
};
struct V{
Point start,end;double ang;
V(Point _start=Point(0,0),Point _end=Point(0,0),double _ang=0.0){
start=_start;end=_end;ang=_ang;
}
friend V operator + (const V &a,const V &b){
return V(a.start+b.start,a.end+b.end);
}
friend V operator - (const V &a,const V &b){
return V(a.start-b.start,a.end-b.end);
}
};
Point Dots[MAXN];
Point Stk[MAXN];int Top;
int n;
double Prellel(double key){
return fabs(key)<EPS?0:key;
}
double Distance(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int Cmp(Point a,Point b){
double res=Prellel(a-Dots[0]^(b-Dots[0]));
if(res>0) return 1;
if(res==0&&Distance(a,Dots[0])<Distance(b,Dots[0])) return 1;
return 0;
}
void Graham(){
int k=0;
for(int i=1;i<n;++i){
if(Dots[i].y<Dots[k].y||(Dots[i].y==Dots[k].y&&Dots[i].x<Dots[k].x)) k=i;
}swap(Dots[0],Dots[k]);sort(Dots+1,Dots+n,Cmp);
Top=1;Stk[0]=Dots[0];Stk[1]=Dots[1];
for(int i=2;i<n;++i){
while(Top>=1&&((Stk[Top]-Stk[Top-1])^(Dots[i]-Stk[Top-1]))<EPS) --Top;
Stk[++Top]=Dots[i];
}Stk[++Top]=Stk[0];
}
double RotateCalipers(){
double ans=-INF32;int q=1;
for(int i=0;i<Top;++i){
while(((Stk[q]-Stk[i+1])^(Stk[i]-Stk[i+1]))<((Stk[q+1]-Stk[i+1])^(Stk[i]-Stk[i+1]))) q=(q+1)%Top;
ans=max(ans,max(Distance(Stk[q],Stk[i]),Distance(Stk[q+1],Stk[i+1])));
}return ans*ans;
}
double Solve(){
double ans=0;
for(int i=0;i<Top;++i){//遍历凸包上的每个点
int j=(i+1)%Top;int k=(j+1)%Top;
//以i,j为边,旋转到最大的k点构成S三角形。
while(k!=i&&fabs((Stk[k]-Stk[j])^(Stk[i]-Stk[j]))<fabs((Stk[(k+1)%Top]-Stk[j])^(Stk[i]-Stk[j]))) k=(k+1)%Top;
// cout<<Top<<" "<<i<<" "<<j<<" "<<k<<endl;
if(k==i) continue;
//找到最大的三角形了 旋转j和k
int stopj=(k+1)%Top;//这里避免了很多重复计算,
while(j!=stopj&&k!=i){//之前写的是while(j!=k&&k!=i)运行时间是1200+ms,优化后是600+ms
double Area1=fabs((Stk[k]-Stk[j])^(Stk[i]-Stk[j]));
double Area2=fabs((Stk[(k+1)%Top]-Stk[j])^(Stk[i]-Stk[j]));
ans=max(ans,max(Area1,Area2));
if(Area1<Area2) k=(k+1)%Top;
else j=(j+1)%Top;
}
}return ans/2.0;
}
int main(){
while(~scanf("%d",&n)){
if(n==-1) break;
for(int i=0;i<n;++i){
scanf("%lf%lf",&Dots[i].x,&Dots[i].y);
}Graham();//构建凸包
printf("%.2f\n",Solve());
}
}
/*
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
*/