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 −10 4 <= xi, yi <= 10 4 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:枚举三角形的一条边,然后通过旋转卡壳找最远的点; 自己想的,而且AC了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define RC rotating_calipers
using namespace std;
const int maxn=;
struct point{
double x,y;
point(double x=,double y=):x(x),y(y){}
bool operator < (const point &c) const { return x<c.x||(x==c.x&&y<c.y);}
point operator - (const point &c) const { return point(x-c.x,y-c.y);}
double operator * (const point &c) const { return x*c.y-y*c.x; }
double operator | (const point &c) const { return (x-c.x)*(x-c.x)+(y-c.y)*(y-c.y); }
};
double det(point A,point B){ return A.x*B.y-A.y*B.x;}
double det(point O,point A,point B){ return det(A-O,B-O);}
point a[maxn],ch[maxn];
void convexhull(int n,int &top)
{
sort(a+,a+n+); top=;
for(int i=;i<=n;i++){
while(top>&&det(ch[top-],ch[top],a[i])<=) top--;
ch[++top]=a[i];
}
int ttop=top;
for(int i=n-;i>=;i--){
while(top>ttop&&det(ch[top-],ch[top],a[i])<=) top--;
ch[++top]=a[i];
}
}
double rotating_calipers(point p[],int top)
{
top--;
double ans=; int now;
rep(i,,top-){
int now=i+;
rep(j,i+,top-){
while(now<=top&&fabs(det(p[i],p[j],p[now]))<fabs(det(p[i],p[j],p[now+]))){
now++;
}
ans=max(ans,fabs(det(p[i],p[j],p[now])));
}
}
return ans;
}
int main()
{
int N;
while(~scanf("%d",&N)&&N!=-){
for(int i=;i<=N;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
int top; convexhull(N,top);
double ans=RC(ch,top);
printf("%.2f\n",0.5*ans);
}
return ;
}

思路2:枚举三角形的一个点,然后通过旋转卡壳找最远的边。别人的代码,AC了,但是拿去做CF的时候WA36了。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define RC rotating_calipers
using namespace std;
const int maxn=;
struct point{
double x,y;
point(double x=,double y=):x(x),y(y){}
bool operator < (const point &c) const { return x<c.x||(x==c.x&&y<c.y);}
point operator - (const point &c) const { return point(x-c.x,y-c.y);}
};
double det(point A,point B){ return A.x*B.y-A.y*B.x;}
double det(point O,point A,point B){ return det(A-O,B-O);}
point a[maxn],ch[maxn];
void convexhull(int n,int &top)
{
sort(a+,a+n+); top=;
for(int i=;i<=n;i++){
while(top>&&det(ch[top-],ch[top],a[i])<=) top--;
ch[++top]=a[i];
}
int ttop=top;
for(int i=n-;i>=;i--){
while(top>ttop&&det(ch[top-],ch[top],a[i])<=) top--;
ch[++top]=a[i];
}
}
double rotating_calipers(point p[],int top)
{
double ans=; int now1=,now2=;
rep(i,,top){
while(fabs(det(p[i],p[now1],p[now2]))<fabs(det(p[i],p[now1],p[now2+]))){
now2++;if(now2==top+1) now2=;
}//利用其是单峰函数
while(fabs(det(p[i],p[now1],p[now2]))<fabs(det(p[i],p[now1+],p[now2]))){
now1++;if(now1==top+1) now1=;
}
ans=max(ans,fabs(det(p[i],p[now1],p[now2])));
}
return ans;
}
int main()
{
int N;
while(~scanf("%d",&N)&&N!=-){
for(int i=;i<=N;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
int top; convexhull(N,top);
double ans=RC(ch,top-);
printf("%.2f\n",0.5*ans);
}
return ;
}

POJ - 2079:Triangle (旋转卡壳,求最大三角形)的更多相关文章

  1. POJ 2079 Triangle 旋转卡壳求最大三角形

    求点集中面积最大的三角形...显然这个三角形在凸包上... 但是旋转卡壳一般都是一个点卡另一个点...这种要求三角形的情况就要枚举底边的两个点 卡另一个点了... 随着底边点的递增, 最大点显然是在以 ...

  2. POJ 2079 Triangle [旋转卡壳]

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 Descript ...

  3. hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)

    链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

  4. CodeForces - 682E: Alyona and Triangles(旋转卡壳求最大三角形)

    You are given n points with integer coordinates on the plane. Points are given in a way such that th ...

  5. poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方

    旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...

  6. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  7. [hdu5251]矩形面积 旋转卡壳求最小矩形覆盖

    旋转卡壳求最小矩形覆盖的模板题. 因为最小矩形必定与凸包的一条边平行,则枚举凸包的边,通过旋转卡壳的思想去找到其他3个点,构成矩形,求出最小面积即可. #include<cstdio> # ...

  8. POJ2187 旋转卡壳 求最长直径

    给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...

  9. POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 7625   Accepted: 2234 Descript ...

  10. poj 2079 Triangle,旋转卡壳求点集的最大三角形

    给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,由于题目n比較大,枚举的话要O(n^3)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...

随机推荐

  1. LeetCode:对角线遍历【498】

    LeetCode:对角线遍历[498] 题目描述 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ ...

  2. 用js来实现那些数据结构 第一章

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...

  3. excel中如何取消自动超链接?

    最近做的表格有点多,年终述职也到了.总有一些地方生疏了,幸好还有点小印象.记录下来,以后可以回来看看. 方法一 适合单个链接的取消 1 输入网址后,按回车键确认,快捷键ctrl+z,即可取消,这种不好 ...

  4. linux alsa pcm(此pcm非硬件pcm接口)

    转:https://blog.csdn.net/crycheng/article/details/7095899 CODEC :音频芯片的控制,比如静音.打开(关闭)ADC(DAC).设置ADC(DA ...

  5. Linux基础三---打包压缩&vim&系统的初始化和服务

    一,常用命令——tar&vim 1. tar [参数]  文件名  [路径] 参数: -c :建立一个压缩文件的参数指令(create 的意思):     -x :解开一个压缩文件的参数指令! ...

  6. 主攻ASP.NET.4.5.1 MVC5.0之重生:根据产品类别显示菜单分类和分页

    路径访问的几种方式和分页效果 显示其它类别的效果和多数据分页效果 默认访问网站路径效果和多数据分页效果 URL路径访问可页面 http://localhost:5339/stationery http ...

  7. 获取本地IP并设置到QLineEdit中

    #include <QHostAddress> #include <QNetworkInterface> #include <QHostInfo> QString ...

  8. 关于centos7下/etc/sysconfig/目录没有iptables问题

    在新买的centos7服务器中想打开防火墙,采用传统centos6的方式用service iptables restart/stop/status 之后报错: 而在/etc/sysconfig/目录下 ...

  9. java 项目中每个jar包的作用总结

    别人的总结 1.Struts2 1)commons-fileupload :2)common-io:文件上传 3)commons-lang:它扩展了标准 java.langAPI ArrayUtils ...

  10. centos下安装Anaconda

    第一步:将下载好的Anaconda2-4.1.1-Linux-x86_64.sh软件传到linux下 第二步:[hadoop@spark1 ~]$ cd Desktop #进入到该软件所在目录,我的放 ...