POJ 2079 Triangle(旋转卡壳计算平面点集最大三角形面积)
来源:互联网 发布:java replace替换成空 编辑:程序博客网 时间:2024/06/05 03:58
Triangle
Time Limit: 3000MS
Memory Limit: 30000KTotal Submissions: 9673
Accepted: 2896
Memory Limit: 30000KTotal Submissions: 9673
Accepted: 2896
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 −104 <= xi, yi <= 104 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
33 42 62 752 63 92 08 06 5-1
Sample Output
0.5027.00
Source
Shanghai 2004 Preliminary
题目大意:给定N个点,求这些点中任意三点围成的最大三角形的面积。
解题思路:先求出一个凸包包围这些点,然后用旋转卡壳求凸包内任意三角形面积比大小。
凸包模版const int MAXN = 50005;Point list[MAXN];int Stack[MAXN],top;bool _cmp(Point p1,Point p2){int tmp = (p1-list[0])^(p2-list[0]); if(tmp > 0)return true; else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0])) return true; else return false;}void Graham(int n){ Point p0; int k = 0; p0 = list[0]; for(int i = 1;i < n;i++) if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x)) { p0 = list[i]; k = i; } swap(list[k],list[0]); sort(list+1,list+n,_cmp); if(n == 1) { top = 1; Stack[0] = 0; return; } if(n == 2) { top = 2; Stack[0] = 0; Stack[1] = 1; return; } Stack[0] = 0; Stack[1] = 1; top = 2; for(int i = 2;i < n;i++) { while(top > 1 &&((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0) top--; Stack[top++] = i; }}
旋转卡壳计算平面点集最大三角形面积模版
int rotating_calipers(Point p[],int n){ int ans = 0; Point v; for(int i = 0;i < n;i++) { int j = (i+1)%n; int k = (j+1)%n; while(j != i && k != i) { ans = max(ans,abs((p[j]-p[i])^(p[k]-p[i]))); while( ((p[i]-p[j])^(p[(k+1)%n]-p[k])) < 0 ) k = (k+1)%n; j = (j+1)%n; } } return ans;}Point p[MAXN];int main(){ int n; while(scanf("%d",&n)==1) { if(n == -1)break; for(int i = 0;i < n;i++)list[i].input(); Graham(n); for(int i = 0;i < top;i++)p[i] = list[Stack[i]]; printf("%.2f\n",(double)rotating_calipers(p,top)/2); } return 0;}
AC代码
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm> using namespace std;struct Point{ int x,y; Point(int _x = 0,int _y = 0) { x = _x; y = _y; } Point operator -(const Point &b)const { return Point(x - b.x, y - b.y); } int operator ^(const Point &b)const { return x*b.y - y*b.x; } int operator *(const Point &b)const { return x*b.x + y*b.y; } void input() { scanf("%d%d",&x,&y); }};//距离的平方int dist2(Point a,Point b){ return (a-b)*(a-b);}const int MAXN = 50005;Point list[MAXN];int Stack[MAXN],top;bool _cmp(Point p1,Point p2){int tmp = (p1-list[0])^(p2-list[0]); if(tmp > 0)return true; else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0])) return true; else return false;}void Graham(int n){ Point p0; int k = 0; p0 = list[0]; for(int i = 1;i < n;i++) if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x)) { p0 = list[i]; k = i; } swap(list[k],list[0]); sort(list+1,list+n,_cmp); if(n == 1) { top = 1; Stack[0] = 0; return; } if(n == 2) { top = 2; Stack[0] = 0; Stack[1] = 1; return; } Stack[0] = 0; Stack[1] = 1; top = 2; for(int i = 2;i < n;i++) { while(top > 1 &&((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0) top--; Stack[top++] = i; }}//旋转卡壳计算平面点集最大三角形面积int rotating_calipers(Point p[],int n){ int ans = 0; Point v; for(int i = 0;i < n;i++) { int j = (i+1)%n; int k = (j+1)%n; while(j != i && k != i) { ans = max(ans,abs((p[j]-p[i])^(p[k]-p[i]))); while( ((p[i]-p[j])^(p[(k+1)%n]-p[k])) < 0 ) k = (k+1)%n; j = (j+1)%n; } } return ans;}Point p[MAXN];int main(){ int n; while(scanf("%d",&n)==1) { if(n == -1)break; for(int i = 0;i < n;i++)list[i].input(); Graham(n); for(int i = 0;i < top;i++)p[i] = list[Stack[i]]; printf("%.2f\n",(double)rotating_calipers(p,top)/2); } return 0;}原题链接http://poj.org/problem?id=2079
阅读全文
0 0
- POJ 2079 Triangle(旋转卡壳计算平面点集最大三角形面积)
- POJ 2079 Triangle (平面点最大三角形 凸包+旋转卡壳 推荐)
- POJ 2079 Triangle(凸包_旋转卡壳之最大三角形面积)
- POJ 2079 Triangle(凸包+旋转卡壳求最大三角形面积)
- POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)
- ZOJ 2419-- Triangle-凸包+旋转卡壳求最大面积三角形(计算几何)
- HDU 3934 Summer holiday(转卡壳计算平面点集最大三角形面积)
- POJ 2079 Triangle (凸包中的最大三角形&旋转卡壳)
- poj 2079 Triangle,旋转卡壳求点集的最大三角形
- POJ 2079 旋转卡壳求最大三角形面积
- 强大的旋转卡壳 POJ 2187 最远点对 POJ 2079点集中面积最大的三角形
- POJ 2079 求最大三角形面积 (凸包+旋转卡壳)
- poj2079 Triangle (旋转卡壳之最大三角形)
- poj 2079 Triangle(旋转卡壳)
- POJ 2079 Triangle(旋转卡壳)
- poj 2079 Triangle (旋转卡壳)
- 旋转卡壳 POJ 2079 Triangle
- POJ 2079 Triangle 旋转卡壳
- Intellij idea设置jdk
- Latex 编译问题
- 2017年PHP程序员未来路在何方
- 记录一次Eclipse出现的奇怪的事情
- ROS实战_1.2 Pioneer 3DX ROSARIA使用教程
- POJ 2079 Triangle(旋转卡壳计算平面点集最大三角形面积)
- 学习PHP注意事项
- 奇技淫巧
- 1330: Five Cards
- sql之left join、right join、inner join的区别
- Hive数据倾斜(大表join大表)
- ccf-201604-1 折点计数
- php生成缩略图的方法
- Extjs4学习笔记(七)----combobox下拉动态树