通道:http://poj.org/problem?id=2079
题意:n个点选面积最大的三角形
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 const int MAX_N = 100007; 8 9 struct Point { 10 double x, y; 11 Point() {} 12 Point(double x, double y) : x(x), y(y) {} 13 Point operator + (const Point& p){ return Point(x + p.x, y + p.y); } 14 Point operator - (const Point& p){ return Point(x - p.x, y - p.y); } 15 Point operator * (const double& d){ return Point(x * d, y * d); } 16 bool operator < (const Point& a) const { 17 if (x != a.x) return x < a.x; 18 else return y < a.y; 19 } 20 }; 21 22 int n, top; 23 Point p[MAX_N], st[MAX_N]; 24 25 double cross(Point p0, Point p1, Point p2) { 26 return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); 27 } 28 29 void covexHull() { 30 sort(p, p + n); 31 st[0] = p[0]; 32 st[1] = p[1]; 33 top = 1; 34 for(int i = 2; i < n; ++i) { 35 while(top > 0 && cross(st[top], p[i], st[top - 1]) <= 0) 36 top--; 37 st[++top] = p[i]; 38 } 39 int temp = top; 40 for(int i = n - 2; i >= 0; --i) { 41 while(top > temp && cross(st[top], p[i], st[top - 1]) <= 0) 42 top--; 43 st[++top] = p[i]; 44 } 45 } 46 47 double A(Point a, Point b, Point c) { 48 return fabs(cross(a, b, c)) / 2; 49 } 50 51 double cal() { 52 covexHull(); 53 int p = 0, q = 1, r = 2; 54 double area = A(st[p], st[q], st[r]); 55 while(true) { 56 int pp = p, qq = q, rr = r; 57 while(fabs(cross(st[(r + 1) % n], st[q], st[p])) > fabs(cross(st[r], st[q], st[p]))) { 58 area = max(area, A(st[(r + 1) % n], st[q], st[p])); 59 r = (r + 1) % n; 60 } 61 while(fabs(cross(st[r], st[(q + 1) % n], st[p])) > fabs(cross(st[r], st[q], st[p]))) { 62 area = max(area, A(st[r], st[(q + 1) % n], st[p])); 63 q = (q + 1) % n; 64 } 65 while(fabs(cross(st[r], st[q], st[(p + 1) % n])) > fabs(cross(st[r], st[q], st[p]))) { 66 area =max(area, A(st[r], st[q], st[(p + 1) % n])); 67 p = (p + 1) % n; 68 } 69 if (pp == p && qq == q && rr == r) r = (r + 1) % n; 70 if(r == 0) break; 71 } 72 return area; 73 } 74 75 int main() { 76 while (1 == scanf("%d", &n)) { 77 if (-1 == n) break; 78 for (int i = 0; i < n; ++i) 79 scanf("%lf%lf", &p[i].x, &p[i].y); 80 double ans = cal(); 81 printf("%.2f\n", ans); 82 } 83 return 0; 84 }