POJ - 2079:Triangle (旋转卡壳,求最大三角形)
Input
Output
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 (旋转卡壳,求最大三角形)的更多相关文章
- POJ 2079 Triangle 旋转卡壳求最大三角形
求点集中面积最大的三角形...显然这个三角形在凸包上... 但是旋转卡壳一般都是一个点卡另一个点...这种要求三角形的情况就要枚举底边的两个点 卡另一个点了... 随着底边点的递增, 最大点显然是在以 ...
- POJ 2079 Triangle [旋转卡壳]
Triangle Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 9525 Accepted: 2845 Descript ...
- hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)
链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS Memory Limit: 30000K Total Submissio ...
- 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 ...
- poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方
旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- [hdu5251]矩形面积 旋转卡壳求最小矩形覆盖
旋转卡壳求最小矩形覆盖的模板题. 因为最小矩形必定与凸包的一条边平行,则枚举凸包的边,通过旋转卡壳的思想去找到其他3个点,构成矩形,求出最小面积即可. #include<cstdio> # ...
- POJ2187 旋转卡壳 求最长直径
给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...
- POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)
Triangle Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 7625 Accepted: 2234 Descript ...
- poj 2079 Triangle,旋转卡壳求点集的最大三角形
给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,由于题目n比較大,枚举的话要O(n^3)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...
随机推荐
- Openstak(M版)控制节点安装
#############修改hosts文件 # controller10.0.0.11 controller# compute110.0.0.31 compute1# block110.0.0.41 ...
- iOS iPhone X 适配启动图片
刚出了Xcode9正式版 迫不及待地下载 使用了 保密了这么久的iPhone X 模拟器 运行起来这个样子 上下都留白不正常 这必须匹配新的启动图才行,马上查苹果开发文档 get it!!!! 添加了 ...
- OS X 与传统Unix的一点区别
在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...
- HTML系列(3)基本的HTML标签(二)
本节继续介绍HTML的常用标签. (1)input标签之文本域(text和textarea).密码域(password): <!DOCTYPE html> <html ...
- P3794 签到题IV
题目 P3794 签到题IV 来切道水题放松一下吧 做法 或是单调不下降的,\(gcd\)是单调不上升的 \(a_i≤5×10^5\)分成权值不同的块数应该很小,所以随便乱搞就出来了 My compl ...
- canvas 视频音乐播放器
canvas 视频音乐播放器 var play_nor_img_path = 'images/play_btn_n.png'; //播放按钮 正常时 60x60 px var play_sec_img ...
- C语言中static的使用方法【转】
本文转自:http://blog.csdn.net/renren900207/article/details/21609649 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量 ...
- hql join
文章一: 1.用hql语句 ` String hql="select student.id, student.name ,class.name from student映射实体类名 as s ...
- Linux学习笔记001——win下安装Linux虚拟机
我研二之前算是一个纯粹的计算机小白,因为某些原因开始接触了计算机方面的知识. Linux系统也就是前几个月才听说,因某些需求需要在Linux环境下运行.纯的Linux系统不太现实, 所以在他人帮助和自 ...
- 到底EJB是什么
到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:" ...