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. Python——函数的高级应用

    一.函数赋值给变量 函数也是对象,也可以赋值给变量,当把函数赋值给变量后,可以通过变量调用函数. 例: def test(x,y): return x * y # 把函数赋值给变量 a = test ...

  2. bootstrap历练实例:面板脚注

    面板脚注 我们可以在面板中添加脚注,只需要把按钮或者副文本放在带有 class .panel-footer 的 <div> 中即可.下面的实例演示了这点: <!DOCTYPE htm ...

  3. skynet 学习笔记-netpack模块(1)

    int luaopen_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop } ...

  4. 简单的Datable转List方法

    public static class DataTableUtils<T> where T : new() { public static List<T> ConvertToM ...

  5. Element表单验证(2)

    Element表单验证(2) 上篇讲的是async-validator的基本要素,那么,如何使用到Element中以及怎样优雅地使用,就在本篇. 上篇讲到async-validator由3大部分组成 ...

  6. C++ 学习笔记(一) cout 与printf 的不同之处

    作为一个嵌入式开发的猿,使用打印调试程序是必不可少的,拿到新的项目第一件事就是捣鼓打印.这次也不例外有打印才有耍下去的底气.在之前零零碎碎的C++学习中,还是一边学一边做项目的状态下能用printf解 ...

  7. 【二分 最小割】cf808F. Card Game

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  8. 使用MySQL yum源安装MySQL

    #首先,将MySQL Yum存储库添加到系统的存储库列表中. #在https://dev.mysql.com/downloads/repo/yum/地址中,下载mysql yum repository ...

  9. python3爬取墨迹天气并发送给微信好友,附源码

    需求: 1. 爬取墨迹天气的信息,包括温湿度.风速.紫外线.限号情况,生活tips等信息 2. 输入需要查询的城市,自动爬取相应信息 3. 链接微信,发送给指定好友 思路比较清晰,主要分两块,一是爬虫 ...

  10. aoj-0118 property distribution(搜索)

    Time limit1000 ms Memory limit131072 kB タナカ氏が HW アールの果樹園を残して亡くなりました.果樹園は東西南北方向に H × Wの区画に分けられ.区画ごとにリ ...