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 −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

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 = +; 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 = ;
Polygon qs(n * );
for (int i = ; i < n;i++) {
while (k > && (qs[k - ] - qs[k - ]).det(ps[i] - qs[k - ]) <= ) k--;
qs[k++] = ps[i];
}
for (int i = n - , t = k; i >= ;i--) {
while (k > t && (qs[k - ] - qs[k - ]).det(ps[i] - qs[k - ]) <= )k--;
qs[k++]=ps[i];
}
qs.resize(k-);
return qs;
} P po[N_MAX];
int N; vector<P> judge_clockwise(vector<P>p) {
for (int i = ; i < p.size()-;i++) {
//double tmp = (p[i + 1] - p[i]).det(p[i + 2] - p[i + 1]);
double tmp = cross(p[i], p[i + ], p[i + ]);
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 = ;
for (int offset = ; offset < (n + ) / ;offset++) {//offset为三角形底边两个顶点的跨度
nnext = (offset + ) % n;
for (int i = ; 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 = ;
int S = triangle_S(s,po[nnext]);
res = max(res, S_MAX);
if (S<= S_MAX)break;
S_MAX = S;
}
if (nnext > )nnext--;//!!!!!!!!!!!!!!!!!!!
else nnext = n - ;
}
}
printf("%d.%s\n", res/ , res % == ? "" : "");
} int main() { while (scanf("%d",&N)&&N!=-) {
for (int i = ; i < N;i++) {
scanf("%d%d",&po[i].x,&po[i].y);
}
Polygon Po = convex_hull(po, N);
solve(Po,Po.size());
}
return ;
}

poj 2079 Triangle的更多相关文章

  1. ●POJ 2079 Triangle

    题链: http://poj.org/problem?id=2079 题解: 计算几何,凸包,旋转卡壳 复杂度O(N^2),(O(N)什么的就不说了,我觉得我看过的O(N)方法正确性都有问题,虽然有些 ...

  2. POJ 2079 Triangle (凸包+旋转卡壳)

    [题目链接] http://poj.org/problem?id=2079 [题目大意] 给出一些点,求出能组成的最大面积的三角形 [题解] 最大三角形一定位于凸包上,因此我们先求凸包,再在凸包上计算 ...

  3. poj 2079 Triangle(旋转卡壳)

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8917   Accepted: 2650 Descript ...

  4. POJ 2079 Triangle [旋转卡壳]

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 Descript ...

  5. POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 7625   Accepted: 2234 Descript ...

  6. poj 2079 Triangle (二维凸包旋转卡壳)

    Triangle Time Limit: 3000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Stat ...

  7. poj 2079 Triangle,旋转卡壳求点集的最大三角形

    给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,由于题目n比較大,枚举的话要O(n^3)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...

  8. POJ 2079 Triangle 旋转卡壳求最大三角形

    求点集中面积最大的三角形...显然这个三角形在凸包上... 但是旋转卡壳一般都是一个点卡另一个点...这种要求三角形的情况就要枚举底边的两个点 卡另一个点了... 随着底边点的递增, 最大点显然是在以 ...

  9. hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)

    链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

随机推荐

  1. JavaScript实用的例子

    ---恢复内容开始--- 1.发送验证码 <input id="send" type="button" value="发送验证码"&g ...

  2. Python——函数入门(一)

    一.理解函数 举一个例子,当我们需要重复使用一个功能的时候,不可能每次都去复制一次代码,这个时候就需要用到函数了,所谓的函数,简单来说就是给函数取一个名字,当需要用到这个功能的时候,就可以通过这个名字 ...

  3. 01_8_session

    01_8_session 1. session总结 1.1服务器的一块内存(存key-value) 1.2和客户端窗口对应(子窗口)(独一无二) 1.3客户端和服务器有对应的SessionID 1.4 ...

  4. 关于removeFromSuperview

    关于  - (void)removeFromSuperview 苹果官网API中是这么描述的: Unlinks the view from its superview and its window, ...

  5. JS数据结构及算法(一) 堆栈

    最近在看<学习JavaScript数据结构与算法>这本书,感觉自己又涨知识了 哈哈... 现在将自己看的做个总结,也是巩固理解. 栈:先进后出,新添加和待删除的元素都保存在栈顶.可以用数组 ...

  6. 【模板】树套树(线段树套Splay)

    如题,这是一个模板... #include <algorithm> #include <iostream> #include <cstring> #include ...

  7. linux关于任务计划

    1.一次性任务计划:at 1)添加 在18:16时候重启服务器 at 18:16 >at init 6 >at ctrl+d2)查看 atq 1 Mon Aug 20 21:09:00 2 ...

  8. ipvsadm启动报错解决方法

    Centos7 yum -y install ipvadm 安装后,启动ipvsadm却报错. Redirecting to /bin/systemctl start ipvsadm.service ...

  9. Python模块(二)(序列化)

    1. namedtuple 命名元组->类似创建了一个类 from collections import namedtuple p = namedtuple("Point", ...

  10. poj1523赤裸裸的割点

    这题真是没什么好说的...赤裸裸的求割点直接模板上 #include<cstdio> #include<cstring> #include<iostream> #i ...