【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 3Output
16.000000Note
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個(gè)點(diǎn),讓你從這些點(diǎn)里面尋找4個(gè)點(diǎn),使得四邊形的面積最大。當(dāng)然題目說了沒有三個(gè)點(diǎn)在一條線上,沒有重合的點(diǎn)。
解題報(bào)告:
? ?這題看起來不難啊,,把四個(gè)點(diǎn)拆成兩個(gè)三角形然后枚舉2+1+1,三重循環(huán)就搞定了,,但是這題還是很坑的啊,,WA23的同黨肯定不少吧,,看這個(gè)簡(jiǎn)單的B題當(dāng)時(shí)cf比賽的時(shí)候比D過題的人都少(不過也可能因?yàn)檫@場(chǎng)的D確實(shí)簡(jiǎn)單,看懂了就是個(gè)模板題)
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;//這么用還是小心點(diǎn)、、、 // 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 */總結(jié):
? sgn函數(shù)要用啊,,,double別直接等號(hào)判斷,,很危險(xiǎn)、、
? ?這是個(gè)坑啊,,還是自己太不小心了、、
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mssearch.exe - mssea
- 下一篇: 美科技巨头每秒能赚多少钱?苹果11376