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)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...
随机推荐
- 合并apk和odex 为完整的apk安装文件
from:http://bbs.hiapk.com/thread-1151284-1-1.html 文件夹:<ignore_js_op> 文件夹拖放到odex.cmd,出现下面的窗口后,按 ...
- JavaScript:学习笔记(3)——正则表达式的应用
JavaScript:正则表达式的应用 应用正则表达式对象RegExp 创建正则表达式 JavaScript中使用RegExp对象来表述一个正则表达式.使用正则表达式之前首先要创建一个RegExp对象 ...
- window.open、window.showModalDialog和window.showModelessDialog 的区别[转]
一.前言 要打开一个可以载入页面的子窗口有三种方法,分别是window.open.window.showModalDialog和window.showModelessDialog. open方法就是打 ...
- C#中利用WebBrowser控件,获得HTML源码
最近获得网页的几个老程序都不能用了. 我原来用 如下代码获得网页html 源码: <pre name="code" class="csharp"> ...
- 【codevs3012+codevs3037】线段覆盖4+线段覆盖5(DP)
线段覆盖4网址:http://codevs.cn/problem/3012/ 线段覆盖5网址:http://codevs.cn/problem/3037/ 题目大意:给出一条直线上的一坨线段,每条线段 ...
- DataX-HDFS(读写)
DataX操作HDFS 读取HDFS 1 快速介绍 HdfsReader提供了读取分布式文件系统数据存储的能力.在底层实现上,HdfsReader获取分布式文件系统上文件的数据,并转换为DataX传输 ...
- NOI2013
Bless All 其实已经没有什么遗憾了呢 下一篇就是OI 再见吧2333
- VBOX不能为虚拟电脑打开一个新任务解决方法
第二种方法亲测有效! http://jingyan.baidu.com/article/4f7d5712da0c131a2119277a.html
- Qt QTreeWidget节点的添加+双击响应+删除详解
转自: http://www.cnblogs.com/Romi/archive/2012/08/08/2628163.html 承接该文http://www.cnblogs.com/Romi/arch ...
- Linux嵌入式 -- 内核 - 系统调用
1. 系统调用 定义 Linux内核中设置了一组用于实现各种系统功能的子程序,称为系统调用.用户可以通过系统调用命令在自己的应用程序中调用它们. 系统调用和普通的函数调用非常相似,区别仅仅在于,系统调 ...