Triangle
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 9835 | Accepted: 2951 |
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 −10
4 <= xi, yi <= 10
4 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
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
题意:平面上给定若干点,求由这些点所组成的三角形中面积最大的三角形。
思路:首先肯定要求出这些店的凸包,三角形的三个定点一定在凸包上。之后考虑如何确定出面积最大的三角形。我们首先固定凸包上其中两个点1,2作为三角形的两个顶点,顶点3则不断的在凸包上运动,一开始运动的时候三角形的面积会逐渐变大,直到运动到某一点使三角形面积达到最大值,之后若顶点3继续运动则三角形面积又开始不断减小,
这时3停止运动,顶点1,2换一个组合,重复上述算法求面积最大值。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> #include<set> #include<cmath> using namespace std; #define EPS 1e-10 #define INF 0x3f3f3f3f const int N_MAX = 50000+16; struct P { int x, y; P(){} P(int x,int y):x(x),y(y) {} P operator +(P p) { return P(x+ p.x,y+ p.y); } P operator -(P p) { return P(x -p.x, y -p.y); } P operator *(P p) { return P(x*p.x, y*p.y); } bool operator <(const P& p)const { if (x != p.x)return x < p.x; else return y < p.y; } int dot(P p) { return x*p.x+y*p.y; } int det(P p) { return x*p.y-y*p.x; } }; bool cmp_x(const P&p,const P&q) { if (p.x != q.x) return p.x < q.x; return p.y < q.y; } struct Segment { P p1, p2; Segment(P p1=P(),P p2=P()):p1(p1),p2(p2) {} }; typedef Segment Line; typedef vector<P>Polygon; inline double cross(P A, P B, P C) { return (B - A).det(C - A); } int triangle_S(Segment s,P p) { return (s.p2 - s.p1).det(p - s.p1); } Polygon convex_hull(P * ps,int n) { sort(ps,ps+n); int k = 0; Polygon qs(n * 2); for (int i = 0; i < n;i++) { while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) k--; qs[k++] = ps[i]; } for (int i = n - 2, t = k; i >= 0;i--) { while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0)k--; qs[k++]=ps[i]; } qs.resize(k-1); return qs; } P po[N_MAX]; int N; vector<P> judge_clockwise(vector<P>p) { for (int i = 0; i < p.size()-2;i++) { //double tmp = (p[i + 1] - p[i]).det(p[i + 2] - p[i + 1]); double tmp = cross(p[i], p[i + 1], p[i + 2]); if (tmp > EPS)return p; else if (tmp < -EPS) { reverse(p.begin(), p.end()); return p; } } return p; } void solve(Polygon po,int n) { int nnext; int res = 0; for (int offset = 1; offset < (n + 1) / 2;offset++) {//offset为三角形底边两个顶点的跨度 nnext = (offset + 1) % n; for (int i = 0; i < n; i++) { int next = (i + offset) % n; Segment s = Segment(po[i], po[next]); int S = triangle_S(s, po[nnext]); int S_MAX = S; for (++nnext; nnext != next&&nnext != i;nnext++) { if (nnext == n)nnext = 0; int S = triangle_S(s,po[nnext]); res = max(res, S_MAX); if (S<= S_MAX)break; S_MAX = S; } if (nnext > 0)nnext--;//!!!!!!!!!!!!!!!!!!! else nnext = n - 1; } } printf("%d.%s\n", res/ 2, res % 2 == 1 ? "50" : "00"); } int main() { while (scanf("%d",&N)&&N!=-1) { for (int i = 0; i < N;i++) { scanf("%d%d",&po[i].x,&po[i].y); } Polygon Po = convex_hull(po, N); solve(Po,Po.size()); } return 0; }