Problem Description

老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大。
Eddy对这道题目百思不得其解,想不通用什么方法来解决,因此他找到了聪明的你,请你帮他解决这个题目。

Input

输入数据包含多组测试用例,每个测试用例的第一行包含一个整数n,表示一共有n个互不相同的点,接下来的n行每行包含2个整数xi,yi,表示平面上第i个点的x与y坐标。你可以认为:3 <= n <= 50000 而且 -10000 <= xi, yi <= 10000.

Output

对于每一组测试数据,请输出构成的最大的三角形的面积,结果保留两位小数。
每组输出占一行。

Sample Input

3
3 4
2 6
3 7
6
2 6
3 9
2 0
8 0
6 6
7 7

Sample Output

1.50
27.00

Author

Eddy

Recommend

lcy

思路:

最大三角形的三个点一定在点的凸包上,求出凸包之后,直接暴力枚举点即可

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 55000;
const int INF = 0x3f3f3f3f;
const int eps = 1e-8;
int sgn(double x)//判断浮点数x的符号,0返回0,正数返回1,负数返回-1
{
if (fabs(x) < eps)return 0;
if (x < 0)return -1;
else return 1;
}
struct Point
{
int x, y;
Point() {}
Point(int _x, int _y)
{
x = _x; y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x, y - b.y);
} double operator ^(const Point &b)const //叉积
{
return x * b.y - y * b.x;
} double operator *(const Point &b)const //点积
{
return x * b.x + y * b.y;
}
bool operator ==(const Point &b)const
{
return (x == b.x) && (y == b.y);
}
bool operator !=(const Point&b)const
{
return (x != b.x) || (y != b.y);
}
void input() { //点的输入
scanf("%d%d", &x, &y);
}
};
struct Line {
Point s, e;
Line() {}
Line(Point _s, Point _e) {
s = _s; e = _e;
}
}; double dist(Point& a, Point& b) //*两点间距离
{
return sqrt((a - b) * (a - b));
}
Point zero;
int k;
bool cmp(Point& a, Point &b)
{
double tmp = (a - zero) ^ (b - zero);
if (sgn(tmp) > 0) {
return 1;
}
if (sgn(tmp) == 0 && sgn(dist(a, zero) - dist(b, zero)) <= 0)
return 1;
return 0;
}
int n;
Point a[MAXN];
int l[MAXN];
int cou = 0;
void Gram()
{
cou = 0;
swap(a[0], a[k]);
sort(a + 1, a + n, cmp);
if (n == 1) {
l[cou++] = 0;
return;
}
if (n == 2) {
l[cou++] = 0;
l[cou++] = 1;
return;
}
l[cou++] = 0;
l[cou++] = 1;
for (int i = 2; i < n; i++) {
while (sgn((a[i] - a[l[cou - 2]]) ^ (a[l[cou - 1]] - a[l[cou - 2]])) != -1) {
cou--;
}
l[cou++] = i;
}
}
int main()
{
//freopen("data.in", "r", stdin);
while (~scanf("%d", &n)) {
zero.x = zero.y = INF;
for (int i = 0; i < n; i++) {
a[i].input();
if (a[i].y < zero.y) {
zero.x = a[i].x;
zero.y = a[i].y;
k = i;
}
else if (a[i].y == zero.y) {
if (a[i].x < zero.x) {
zero.x = a[i].x;
zero.y = a[i].y;
k = i;
}
}
}
Gram();
double res = -3;
for (int i = 0; i < cou; i++) {
for (int j = 0; j < cou; j++) {
for (int k = 0; k < cou; k++) {
double tmp = fabs((a[l[i]] - a[l[j]]) ^ (a[l[k]] - a[l[j]]));
if (tmp > res) {
res = tmp;
}
}
}
}
res = res / 2 ;
printf("%.2lf\n", res);
}
}

HDU2202--最大三角形(凸包,枚举)的更多相关文章

  1. poj1873 The Fortified Forest 凸包+枚举 水题

    /* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...

  2. poj 1873 凸包+枚举

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1 ...

  3. hdu 4709:Herding(叉积求三角形面积+枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. hdu 2202 最大三角形 (凸包)

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

  5. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest

    题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...

  6. UVa1453或La4728 凸包+枚举(或旋转卡壳)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. POJ 1873 The Fortified Forest [凸包 枚举]

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6400   Accepted: 1 ...

  8. BZOJ 1201 [HNOI2005]数三角形:枚举 + 前缀和

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1201 题意: 有一个边长为n的正三角形网格,去掉其中一些线段,问你在这幅图中有多少个三角形 ...

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

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

  10. hdu 最大三角形(凸包+旋转卡壳)

    老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy对这道题目百思不得其解,想不通用什么方法 ...

随机推荐

  1. MSSQL2008 中文乱码问题 (引自ljg888的专栏)

    PHP向MSSQL2008中写入数据,中文乱码   首先:查看SQLserver编码格式的SQL语句为:     SELECT  COLLATIONPROPERTY('Chinese_PRC_Stro ...

  2. Zsh安装

    Zsh 使用 Homebrew 完成 zsh 和 zsh completions 的安装 brew install zsh zsh-completions 安装 oh-my-zsh 让 zsh 获得拓 ...

  3. C++友元

    通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. 例1: #include<iostream>using namespace ...

  4. JavaScript Date对象更进一步

    总结分享这个近期开发解决的一个Bug. Javascript的Date对象具有容错性,会自动根据当年的日期根据设置的属性值转换,也就是说Date对象的setDate会影响setMonth,month会 ...

  5. cpu affinity (亲和性)

    来源:http://www.ibm.com/developerworks/cn/linux/l-affinity.html#download 管理处理器的亲和性(affinity) 为什么(3 个原因 ...

  6. WCF发布错误及解决方案

    一:在本机直接运行时出错 使用WCF写了一个小程序测试一下它的功能在运行时报错.“添加服务失败.服务元数据可能无法访问.请确保服务正在运行并且正在公开元数据.” 如下图所示: 查了下资料把它解决了,记 ...

  7. vs2010帮助文档下载以及帮助查看器(H3Viewer)的使用

    在工作中遇到想查看vs2010的帮助文档.推荐使用H3Viewer.一个第三方的免费软件,独立于VS2010运行的帮助查看器.这方面的资料并不多.把本次自己使用的心得分享给大家. H3Viewer官方 ...

  8. spring的校验框架 @Validated & BindingResult

    controller上写法类似这样: @RequestMapping(value = "saleInfoList.json", method = RequestMethod.GET ...

  9. sql 查看语句的性能

    SET STATISTICS TIME:看cpu时间   SET STATISTICS IO:关注scan count(计数)------查询读取的表数量:logical read( 逻辑读)次数

  10. java设计模式(二)

    抽象工厂模式 对工厂同一抽象,便于扩展 interface Provider{ public Sender Send(); } class MailFactory implements Provide ...