【CodeForces - 340B 】Maximal Area Quadrilateral (計算幾何,列舉,有坑)
題幹:
Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral.
Input
The first line contains integer n (4 ≤ n ≤ 300). Each of the next n lines contains two integers: xi, yi ( - 1000 ≤ xi, yi ≤ 1000) — the cartesian coordinates of ith special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide.
Output
Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10 - 9.
Examples
Input
5 0 0 0 4 4 0 4 4 2 3
Output
16.000000
Note
In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4 = 16.
題目大意:
給你n個點,讓你從這些點裡面尋找4個點,使得四邊形的面積最大。當然題目說了沒有三個點在一條線上,沒有重合的點。
解題報告:
這題看起來不難啊,,把四個點拆成兩個三角形然後列舉2+1+1,三重迴圈就搞定了,,但是這題還是很坑的啊,,WA23的同黨肯定不少吧,,看這個簡單的B題當時cf比賽的時候比D過題的人都少(不過也可能因為這場的D確實簡單,看懂了就是個模板題)
AC程式碼:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const double eps = 1e-8;
int sgn(double x) {
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
return 1;
}
struct Point {
double x,y;
int id;
Point() {}
Point(double x,double y):x(x),y(y) {}
Point operator -(const Point &b)const {return Point(x - b.x,y - b.y);}
double operator ^(const Point &b)const {return x*b.y - y*b.x;}
double operator *(const Point &b)const {return x*b.x + y*b.y;}
} p[1005];
double ans,ans1,ans2;
int main()
{
int n;
cin>>n;
for(int i = 1; i<=n; i++) scanf("%lf%lf",&p[i].x,&p[i].y),p[i].id = i;
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=n; j++) {
ans1=ans2=-1.0;
for(int k = 1; k<=n; k++) {
if(k == i || k == j) continue;
double tmp = (p[k]-p[i])^(p[j]-p[i]);
if(sgn(tmp) > 0) ans1 = max(ans1,tmp);
if(sgn(tmp) < 0) ans2 = max(ans2,-tmp);
}
if(ans1==-1.0 || ans2 == -1.0) continue;//這麼用還是小心點、、、
// cout << i << "***"<<j<< "***";
// cout << ans1+ans2<<endl;
ans = max(ans,ans1+ans2);
}
}
printf("%.8f\n",ans/2);
return 0 ;
}
/*
4
0 0
0 5
5 0
1 1
*/
總結:
sgn函式要用啊,,,double別直接等號判斷,,很危險、、
這是個坑啊,,還是自己太不小心了、、