POJ2079:Triangle——题解
http://poj.org/problem?id=2079
题目大意:求最大面积的三角形。
——————————————————
可以知道,最大面积的三角形的顶点一定是最大凸包的顶点。
接下来就是O(n*n)的常数优化题了(利用单峰性)。
(但其实不是n*n的,因为我们求的是纯凸包,所以n会小一些)
#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double dl;
const dl eps=1e-;
const int N=;
struct point{
dl x;
dl y;
}p[N],q[N];
int n,per[N],l;
inline point getmag(point a,point b){
point s;
s.x=b.x-a.x;s.y=b.y-a.y;
return s;
}
inline dl multiX(point a,point b){
return a.x*b.y-b.x*a.y;
}
inline dl dis(point a,point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline bool cmp(int u,int v){
dl det=multiX(getmag(p[],p[u]),getmag(p[],p[v]));
if(fabs(det)>eps)return det>eps;
return dis(p[],p[u])-dis(p[],p[v])<-eps;
}
void graham(){
int id=;
for(int i=;i<=n;i++){
if(p[i].x-p[id].x<-eps||(fabs(p[i].x-p[id].x)<eps&&p[i].y-p[id].y<-eps))id=i;
}
if(id!=)swap(p[],p[id]);
for(int i=;i<=n;i++)per[i]=i;
sort(per+,per+n+,cmp);
l=;
q[++l]=p[];
for(int i=;i<=n;i++){
int j=per[i];
while(l>=&&multiX(getmag(q[l-],p[j]),getmag(q[l-],q[l]))>-eps){
l--;
}
q[++l]=p[j];
}
return;
}
inline dl area(){
if(l<=)return ;
dl ans=;
for(int i=;i<=l;i++){
int j=i%l+;
int k=j%l+;
while(){
dl s1=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
dl s2=multiX(getmag(q[i],q[j]),getmag(q[i],q[k%l+]));
if(fabs(s1)-fabs(s2)>-eps){
break;
}
k=k%l+;
}
while(i!=j&&j!=k&&i!=k){
dl s=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
ans=max(ans,fabs(s)/2.0);
while(){
dl s1=multiX(getmag(q[i],q[j]),getmag(q[i],q[k]));
dl s2=multiX(getmag(q[i],q[j]),getmag(q[i],q[k%l+]));
if(fabs(s1)-fabs(s2)>-eps){
break;
}
k=k%l+;
}
j=j%l+;
}
}
return ans;
}
int main(){
while(scanf("%d",&n)!=EOF&&n!=-){
for(int i=;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
graham();
printf("%.2f\n",area());
}
return ;
}
POJ2079:Triangle——题解的更多相关文章
- ZOJ 4081 Little Sub and Pascal's Triangle 题解
ZOJ 4081 Little Sub and Pascal's Triangle 题解 题意 求杨辉三角第n行(从1开始计数)有几个奇数. 考察的其实是杨辉--帕斯卡三角的性质,或者说Gould's ...
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
- POJ2079 Triangle
题面 题解 我什么时候会过这种东西???(逃 旋转卡壳板子题(听说这个算法有十六种读音??? 我是真的忘了这道题目怎么做了,挂个\(blog\),等我学会了再写题解 我的代码里居然有注释???好像还是 ...
- CF336A Vasily the Bear and Triangle 题解
Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- CF1064A Make a triangle! 题解
Content 有三条长度分别为 \(a,b,c\) 的线段.你可以在一个单位时间内将一条线段的长度增加 \(1\),试求出能使这三条线段组成一个三角形的最短时间. 数据范围:\(1\leqslant ...
- POJ 1927 Area in Triangle 题解
link Description 给出三角形三边长,给出绳长,问绳在三角形内能围成的最大面积.保证绳长 \(\le\) 三角形周长. Solution 首先我们得知道,三角形的内切圆半径就是三角形面积 ...
- 120. Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- Triangle leetcode java
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
随机推荐
- zedboard学习第一篇
1. 刚开始学习使用,不知道从哪里开始,手上的资料也很乱,至于这个板子需要学什么也不清楚. 2. 第一个工程就从helloworld开始吧,Zed板上的Zynq是一个PS(processing sys ...
- linux下实现ssh无密码登录访问
在192.168.9.51机器上 1)运行:#ssh-keygen -t rsa 2)然后拍两下回车(均选择默认) 3)运行: #ssh-copy-id -i /root/.ssh/id_rsa.pu ...
- redhat防火墙管理
systemctl status firewalldsystemctl stop firewalldsystemctl start firewalldsystemctl enable firewall ...
- focus如何实现事件委托
事件委托是利用事件冒泡机制的一种优化手段,如果有很多列表元素要绑定事件,那么就可以用事件委托来优化(不需要给每个元素都绑定事件).但是对于focus这种特殊的表单事件,它不会冒泡,那么又该如何实现这一 ...
- WPF & EF & Prism useful links
Prism Attributes for MEF https://msdn.microsoft.com/en-us/library/ee155691%28v=vs.110%29.aspx Generi ...
- 孤荷凌寒自学python第八十二天学习爬取图片2
孤荷凌寒自学python第八十二天学习爬取图片2 (完整学习过程屏幕记录视频地址在文末) 今天在昨天基本尝试成功的基础上,继续完善了文字和图片的同时爬取并存放在word文档中. 一.我准备爬取一个有文 ...
- codeforces 301D Yaroslav and Divisors(树状数组)
Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he ...
- 左值&右值
一.引子 我们所谓的左值.右值,正确的说法应该是左值表达式.右值表达式. 因为C++的表达式不是左值就是右值. 在C中,左值指的是既能够出现在等号左边也能出现在等号右边的表达式,右值指的则是只能出现在 ...
- PART1 一些想法
其实我一直是一个后知后觉的人,这点也是我过了好久才发现的问题,之所以晚发现自己这个毛病,是因为后知后觉==,这有点像是个悖论或者是笑话,但的确是真实存在于我的身上.其实当初为啥来这个学校选计算机的专业 ...
- MFC消息处理
1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook().这个Hook将拦截 ...