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 ;
}

最新文章

  1. 简单动态规划-LeetCode198
  2. [LeetCode] Text Justification 文本左右对齐
  3. 汇总常用的jQuery操作Table tr td方法
  4. boost环境搭建
  5. 别人网站生成的json
  6. OVS操作总结
  7. app.config应该放哪?
  8. 异步加载图片到GridView上,防止OOM
  9. Android屏幕图标尺寸规范
  10. 孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网
  11. java :equals()和hashcode()方法的结合使用
  12. python3 装饰器初识 NLP第三条
  13. ssh_exchange_identification: read: Connection reset by peer
  14. php7 使用dom动态生成xml文档
  15. [转]java实现,输入数据,空格继续,回车结束输入
  16. WPF设置对象隐藏、不可用
  17. history 命令历史
  18. zabbix3.4.7集成grafana详细步骤
  19. python 实验环境
  20. TF 设置GPU模式训练

热门文章

  1. maven课程 项目管理利器-maven 3-1 maven常用的构建命令
  2. Jquery插件之ajaxForm简介
  3. mongodb客户端操作常用命令
  4. 通过adb获取应用的Activity堆栈信息
  5. SpringMVC ------JstlView
  6. Tomcat配置文件server.xml分析
  7. 特殊矩阵的压缩存储(转自chunlanse2014)
  8. testng失败重跑
  9. 搭建FTP服务
  10. 有一个form,包含两个text,和两个按钮,当用户按第一个按扭时把数据提交到url1,按第二个按钮提交到url2,怎么实现呀?