HDU2202--最大三角形(凸包,枚举)
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--最大三角形(凸包,枚举)的更多相关文章
- poj1873 The Fortified Forest 凸包+枚举 水题
/* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...
- poj 1873 凸包+枚举
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6198 Accepted: 1 ...
- hdu 4709:Herding(叉积求三角形面积+枚举)
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 2202 最大三角形 (凸包)
最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...
- UVa1453或La4728 凸包+枚举(或旋转卡壳)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- POJ 1873 The Fortified Forest [凸包 枚举]
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6400 Accepted: 1 ...
- BZOJ 1201 [HNOI2005]数三角形:枚举 + 前缀和
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1201 题意: 有一个边长为n的正三角形网格,去掉其中一些线段,问你在这幅图中有多少个三角形 ...
- (hdu step 7.1.6)最大三角形(凸包的应用——在n个点中找到3个点,它们所形成的三角形面积最大)
题目: 最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu 最大三角形(凸包+旋转卡壳)
老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy对这道题目百思不得其解,想不通用什么方法 ...
随机推荐
- NOIP2015-普及组复赛-第一题-金币
题目描述 Description 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币:之后四天( ...
- Scala Apply
class ApplyTest{ //一定要写(),不加括号就报错. def apply() = println("Into Spark!") def havaAtry(){ pr ...
- CentOS7 citus9.5 集群安装及管理
1 所有节点配置 #------服务安装 服务yum update -y #------扩展依赖安装yum install -y epel-release && yum update ...
- C#模板打印excel
using Microsoft.Office.Interop.Excel; //引用 public void PrintPriviewExcelFile(string filePath) { ...
- 【实验室笔记】C#的Socket客户端接收和发送数据
采用socket发送和接收数据的实验中,服务器采用的是网络助手作为模拟服务器端. 客户端程序流程: 应用的命名空间: using System.Net; using System.Net.Socket ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
- ubuntu下百度云安装
1.安装 在github上下载 .deb 安装包 下载地址 https://github.com/LiuLang/bcloud-packages 下载完成双击安装文件安装成功 2.解决验证码问题 (1 ...
- Mapreduce参数调节
http://blog.javachen.com/2014/06/24/tuning-in-mapreduce/ 本文主要记录Hadoop 2.x版本中MapReduce参数调优,不涉及Yarn的调优 ...
- 第六十五,html嵌入元素
html嵌入元素 学习要点: 1.嵌入元素总汇 2.嵌入元素解析 本章主要探讨HTML5中嵌入元素,嵌入元素主要功能是把外部的一些资源插入到HTML中. 一.嵌入元素总汇 ...
- 关于Unity项目中创建项目遇到的一些问题
1.Unity调用Android的方法默认不是在UI线程执行,所以在Android上写一些页面的重绘的方法,让Unity去调用时,注意要在Android中添加对应的runOnUiThread才可以: ...