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. Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.

    发生情况:将thinkPHP从官网上下了  http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...

  2. c++ 读取文件 最后一行读取了两次

    用ifstream的eof(),竟然读到文件最后了,判断eof还为false.网上查找资料后,终于解决这个问题. 参照文件:http://tuhao.blogbus.com/logs/21306687 ...

  3. C++ 学习笔记 开篇

    从大一开始学习C语言,大学期间做了许多嵌入式的开发项目,毕业后从事嵌入式开发工作主要的开发语言也是C语言.虽然期间断断续续的学习过C++,做过QT.C#上位机但也只是在其他语言的外壳下使用C在开发,始 ...

  4. k8s Pod的自动水平伸缩(HPA)

    我们知道,当访问量或资源需求过高时,使用:kubectl scale命令可以实现对pod的快速伸缩功能 但是我们平时工作中我们并不能提前预知访问量有多少,资源需求多少. 这就很麻烦了,总不能为了需求总 ...

  5. salt 模板

    http://www.mamicode.com/info-detail-2297406.html

  6. Qt:实现子线程发送信号父线程切换图片

    mainwindow.h中代码 #ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include " ...

  7. C# WPF 粘贴板记录器

    工作学习中需要搜索很多资料,有建立文档对遇到过的问题进行记录,但是一来麻烦,二来有些当时认为不重要的事情,也许一段时间后认为是重要的,需要记录的,却又一时找不到,浪费时间做重复的事情.正好借着这个机会 ...

  8. 基于axios的vue插件,让http请求更简单

    ajax-plus 基于axios 的 Vue 插件 如何使用 npm 模块引入 首先通过 npm 安装 ```npm install --save ajax-plus or yarn add aja ...

  9. 第7课 Thinkphp 5 模板输出变量使用函数 Thinkphp5商城第四季

    目录 1. 手册地址: 2. 如果前面输出的变量在后面定义的函数的第一个参数,则可以直接使用 3. 还可以支持多个函数过滤,多个函数之间用"|"分割即可,例如: 4. 变量输出使用 ...

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

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