Triangle

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 <= 104for 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

Source

凸包三角:求N个点组成的三角形的最大面积?

思路:

不难想到最大三角形一定由凸包的顶点构成,难点在于怎么搜索。O(N^3)枚举会超时,旋转卡壳法O(N^2)解决问题。

点的移动:先固定一条边(红色实线),然后依次搜索第3个顶点1st,三角形面积必然是先增后减的,一旦发现开始减小了,立即终止搜索,转而移动固定边。

边的移动:固定边的移动也有讲究,固定边两点的跨度用add表示的话,add不一定是1,最大可达到(N + 1)/2,于是将此add也枚举一遍即可。

代码:

 #include "cstdio"
#include "map"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "iostream"
#include "algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<n;i++)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int mOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
const int inf=0x3f3f3f3f;
using namespace std;
struct P
{
db x, y;
P() {}
P(db x, db 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 * (db d){ return P(x*d, y*d); }
bool operator < (const P& a) const
{
if (x != a.x) return x < a.x;
else return y < a.y;
}
db dot(P p) { return x*p.x + y*p.y; }
db det(P p) { return x*p.y - y*p.x; }
}; P p[N];
// 向量AB 与 AC 的叉积 如果叉积大于0,那么C在向量AB的逆时针方向,叉积小于0则在AB的顺时针方向。如果叉积等于0,则ABC共线。
db cross(P A, P B, P C) {return (B - A).det(C - A); }
// AB和AC构成的平行四边形面积
db Area(P A, P B, P C) {return abs(cross(A, B, C)); }
// 求凸包
vector <P> ch(P *ps, int n)
{
sort(ps, ps + n);
int k = ; // 凸包的顶点数
vector <P> 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;
} int main()
{
int n;
while (~scanf("%d", &n) && n > )
{
for(int i = ; i < n; ++i) cd(p[i].x),cd(p[i].y);
vector <P> ps = ch(p, n);
n = ps.size();
db ans = ;
for(int ad = ; ad < (n + ) / ; ++ad)
{
int k = (ad + ) % n;
for(int i = ; i < n; ++i)
{
int j = (i + ad) % n;
db prev = Area(ps[i], ps[j], ps[k]);
for(++k; k != j && k != i; ++k)
{
if (k == n) k = ;
db cur = Area(ps[i], ps[j], ps[k]);
ans = max(ans, prev);
if (cur <= prev) break; // 达到极值
prev = cur;
}
--k; // 退出循环时,其实k已经超了一个,这里减回来
if(k == -) k += n;
}
}
printf("%.2f\n", ans / );
}
return ;
}

POJ 2079 最大三角形面积(凸包)的更多相关文章

  1. poj 2079(旋转卡壳求解凸包内最大三角形面积)

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9060   Accepted: 2698 Descript ...

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

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

  3. (hdu step 7.1.6)最大三角形(凸包的应用——在n个点中找到3个点,它们所形成的三角形面积最大)

    题目: 最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

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

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

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

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

  6. ●POJ 2079 Triangle

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

  7. poj 2079 Triangle(旋转卡壳)

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

  8. HDU 2202 最大三角形(凸包)

    Problem Description 老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy ...

  9. POJ 2079 Triangle [旋转卡壳]

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

随机推荐

  1. sqlserver门户设置

    ------ insert by wandz 20180918 门户模板表 start ------set identity_insert oa_portal_template on;begin de ...

  2. 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification

    这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...

  3. Struts2_HelloWorld_7_2

    第一个程序的流程图: Struts2.x 的作用:把请求和展现分开.

  4. 谨慎使用#pragma pack

    前段时间将一个项目由vc6.0转为vs2005,发现了有些对象的地址奇怪变化的问题,细查之下发现出现了#pragma pack乱用的问题,在恢复内存对齐使用了#pragma pack(pop, 1)的 ...

  5. HTML?这些还不懂咋办?

    1.什么是空白折叠现象?为什么要空白折叠呢? 对于我们大多数人的习惯来讲,大都喜欢利用空格或者换行来调整文章的文字结构.这样往往可以使我们可以更轻松的阅读.但是,在HTML中却不允许我们这么做,这是为 ...

  6. IOS 设置ipone状态栏的样式

    /** 控制状态栏的样式 */ -(UIStatusBarStyle)preferredStatusBarStyle { //白色 return UIStatusBarStyleLightConten ...

  7. [论文理解]关于ResNet的进一步理解

    [论文理解]关于ResNet的理解 这两天回忆起resnet,感觉残差结构还是不怎么理解(可能当时理解了,时间长了忘了吧),重新梳理一下两点,关于resnet结构的思考. 要解决什么问题 论文的一大贡 ...

  8. centos 开启http代理tinyproxy

    一.前言 就算有一些公司想到要进行压力测试也是用一些微软,官网出的一些软件,一个ip发起很多访问.等有一天黑客攻击来了发现还是顶不住.华盟君认为知此知彼才是压力测试的关键点,应当模拟黑客手法进行压力测 ...

  9. 问题 B: 投简历

    题目描述 小华历经12寒窗苦读,又经历4年大学磨砺,终于毕业了,随着毕业季的到来,找工作也日益紧张起来.由于要面试不同的公司,因此小华需要准备不同的简历.当然最基本的信息是必不可少的,基本信息:姓名. ...

  10. VC-基础:VS2010/MFC-1MFC消息映射

    Windows消息分类 Windows消息分为系统消息和用户自定义消息.Windows系统消息有三种: 1.标准Windows消息.除WM_COMMAND外以WM_开头的消息是标准消息.例如,WM_C ...