1. 程式人生 > >[SCOI2007]最大土地面積

[SCOI2007]最大土地面積

bool inpu gpo body desc graham 坐標 -a span

Description

  在某塊平面土地上有N個點,你可以選擇其中的任意四個點,將這片土地圍起來,當然,你希望這四個點圍成
的多邊形面積最大。

Input

  第1行一個正整數N,接下來N行,每行2個數x,y,表示該點的橫坐標和縱坐標。

Output

  最大的多邊形面積,答案精確到小數點後3位。

Sample Input

5
0 0
1 0
1 1
0 1
0.5 0.5

Sample Output

1.000

HINT

數據範圍 n<=2000, |x|,|y|<=100000

首先最大的4個點一定在凸包上

先把凸包求出來

然後選定對角線,然後發現另外兩個頂點是單調的,用旋轉卡殼的思想

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 struct point
 8 {
 9   double x,y;
10 }p[5001],s[5001];
11 int n,top;
12 double ans,eps=1e-5;
13 int dcmp(double x)
14 {
15
if (x<-eps) return -1; 16 if (x>eps) return 1; 17 return 0; 18 } 19 double operator *(point a,point b) 20 { 21 return a.x*b.y-a.y*b.x; 22 } 23 point operator -(point a,point b) 24 { 25 return (point){a.x-b.x,a.y-b.y}; 26 } 27 double dist(point a,point b) 28 { 29 return
(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 30 } 31 bool cmp(point a,point b) 32 { 33 return (a.y<b.y)||(a.y==b.y&&a.x<b.x); 34 } 35 bool cmp2(point a,point b) 36 { 37 int t=dcmp((p[1]-a)*(p[1]-b)); 38 if (t==0) return dist(p[1],a)<dist(p[1],b); 39 return t>0; 40 } 41 void graham() 42 {int i; 43 sort(p+1,p+n+1,cmp); 44 sort(p+2,p+n+1,cmp2); 45 s[++top]=p[1];s[++top]=p[2]; 46 for (i=3;i<=n;i++) 47 { 48 while (top>1&&dcmp((p[i]-s[top-1])*(s[top]-s[top-1]))>=0) top--; 49 s[++top]=p[i]; 50 } 51 } 52 void solve() 53 {int i,j; 54 s[top+1]=s[1]; 55 for (i=1;i<=top-2;i++) 56 { 57 int a=i%top+1,b=(i+2)%top+1; 58 for (j=i+2;j<=top;j++) 59 { 60 while (a%top+1!=j&&dcmp((s[a+1]-s[i])*(s[j]-s[i])-(s[a]-s[i])*(s[j]-s[i]))>0) a=a%top+1; 61 while (b%top+1!=i&&dcmp((s[j]-s[i])*(s[b+1]-s[i])-(s[j]-s[i])*(s[b]-s[i]))>0) b=b%top+1; 62 //cout<<i<<‘ ‘<<a<<‘ ‘<<j<<‘ ‘<<b<<endl; 63 ans=max(ans,(s[a]-s[i])*(s[j]-s[i])+(s[j]-s[i])*(s[b]-s[i])); 64 } 65 } 66 } 67 int main() 68 {int i; 69 cin>>n; 70 for (i=1;i<=n;i++) 71 { 72 scanf("%lf%lf",&p[i].x,&p[i].y); 73 } 74 graham(); 75 solve(); 76 printf("%.3lf\n",ans/2.0); 77 }

[SCOI2007]最大土地面積