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)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...
随机推荐
- 常见Web源码泄露总结
来自:http://www.hacksec.cn/Penetration-test/474.html 摘要 背景 本文主要是记录一下常见的源码泄漏问题,这些经常在web渗透测试以及CTF中出现. .h ...
- 使用JDK将tomcat变成https访问
1,今日JDK目录,执行命令 keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore " ...
- @MarkFan 口语练习录音 20140415 [MDL演讲口语录音]
Hi,everybody! 今天是2014年4月14日, 现在是晚上十一点零柒分. 一本励志的书,一场振奋人心的演讲,一次推心置腹的谈话, 最多只是在你背后小推你一下,最终决定是否迈出前进的步伐, 以 ...
- 数据库系统概论学习2-《关系数据库与 E/R 模型》
-----------------------------一直更新学习内容------------------------------------ 建立一个关系数据库需要几步? 2.关系数据库与 E/ ...
- python装饰器实现HTTP请求耗时和入参返回日志记录
装饰器方法: 1 def decoArgs(server_name): 2 def deco(func): 3 def wrapper(view, request, *args, **kwargs): ...
- maven setting.xml 存放位置导致deply失败,显示没有权限401错误
settings.xml存在于两个地方: 1.安装的地方:$M2_HOME/conf/settings.xml 2.用户的目录:${user.home}/.m2/settings.xml 我只在mav ...
- Kubernetes TLS认证
转自: https://mritd.me/2018/01/07/kubernetes-tls-bootstrapping-note/ 前段时间撸了一会 Kubernetes 官方文档,在查看 TLS ...
- Spark 总结2
网页访问时候 没有打开 注意防火墙! 启动park shell bin下面的spark-shell 这样启动的是单机版的 可以看到没有接入集群中: 应该这么玩儿 用park协议 spark:/ ...
- php+JS进度条
<?phpini_set('max_execution_time','0');//设置本页面加载时间无限制 echo "<div style='border: 1px solid ...
- 让外界可以访问电脑上的网站的几种方式——花生壳,域名,IIS(待)
前话: 每次“养大“一个网站,都有种骄傲地想秀给朋友们看的冲动. 之前可能是困于电脑,实在不方便. 现在,不用克制了! 该秀就秀,能装逼就装逼. 养大孩子就该拉出来秀秀,见见世面. 正题:这次实习,我 ...