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)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...
随机推荐
- Protobuf支持 pointf
Protobuf支持 pointf序列化 加入:ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(System.Drawing.PointF), fa ...
- Centos 常用系统命令
一.查看系统硬件信息: 1.CPU # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 c ...
- 寻找最大(小)的K个数
<<编程之美>>一书中提到了寻找最大的K个数的问题,问题可以简单描述为:在长度为N的数组中,寻找第K(K<N)个最大的数.问题的解法涉及到了很多排序算法,对我们理解和运用 ...
- Redis集群环境搭建
Redis集群cluster环境搭建 描述:本章节主要单服务器搭建集群,在一个服务器上启动多个不同端口的redis服务,非真实环境. 真实环境下redis集群会搭建在多个物理服务器上,并非单一的服务器 ...
- 主攻ASP.NET.4.5 MVC4.0之重生:图书推荐
前段时间看完ASP.Net4.0 框架揭秘 ,目前现在此书在家睡大觉,看得云里雾里,实战有些东西用不上,感觉好可惜. 大概看了一下这本书,这本书很多功能,可以在实际项目中能用上的方法和技巧.小小推荐, ...
- 20145210姚思羽《网络对抗》Web基础
20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...
- 20145231《Java程序设计》第三次实验报告
实验三 敏捷开发与XP实现 实验内容 XP基础 XP核心实践 相关工具 实验要求 了解敏捷开发的基本方法以及XP软件开发的相关准则:了解代码编写的标准和原则:体会结对编程的好处: 实践体会版本控制的方 ...
- 4.2《深入理解计算机系统》笔记(五)并发、多进程和多线程【Final】
该书中第11章是写web服务器的搭建,无奈对web还比较陌生.还没有搞明白. 这些所谓的并发,其实都是操作系统做的事情,比如,多进程是操作系统fork函数实现的.I/O多路复用需要内核挂起进程.多线程 ...
- [RK3288][Android6.0] 调试笔记 --- user版本默认显示开发者选项【转】
本文转载自:https://blog.csdn.net/kris_fei/article/details/70157137 Platform: ROCKCHIPOS: Android 6.0Kerne ...
- UVA639 二叉树
题意:深度为n的二叉树每个节点上有个开关,初始为关闭,每当小球落在节点上都会改变开关的状态,问编号为m的小球最终会落在哪里. 思路:对于二叉树的节点k,左节点右节点的编号为2k,2k+1.只需由最后一 ...