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 ...
随机推荐
- LiteOS创建任务的一个BUG
在任务创建的时候,参数无法传递,第二个参数本来是用来做参数传递的,但是却没用到,很尴尬啊,缺少了这个功能,很多无法写了? osThreadId_t osThreadNew (osThreadFunc_ ...
- OSG-粒子系统和初步
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- 微信小程序入门学习之事件 事件对象 冒泡非冒泡事件(1)
这关于事件的学习,可以自己复制到微信开发者工具上自己运行试试. 首先这里有两个文件.js 和.wxml 文件 首先给出.js文件下代码 // pages/news/news.js Page({ /** ...
- 【循环控制器】-(针对中间部分要循环的场景,相当于loadrunner的action部分)
一般使用 setup线程组 + teardown组 针对中间要循环的部分 使用循环处理器 单独循环中间的部分,相当于loadrunner的action部分
- truffle框架快速开发合约步骤
矩阵元区块链智能合约开发指南 1 适用范围 本规范描述了矩阵元区块链系统智能合约的开发约束与规范,用以指导DAPP开发者按照本规范开发基于矩阵元区块链运行的应用. 2 术语解释 术语 术语解释 DAP ...
- 第一届"进化论杯"月赛 解题报告
Problem A: derivative 思路:水题.算出二阶导数,直接 printf 结果. 在求出二阶导数后可以不立刻化简,此时式中带有大量 e^(-x) 项.此时直接可以代入 ln|x0|,把 ...
- android BadgeView的使用(图片上的文字提醒)
BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及显示的位置等等: 这是Gi ...
- ubuntu 安装 hustoj
https://github.com/zhblue/hustoj 准备工作: http://www.java123.net/v/961634.html 1.首先打开命令行,切换到root身份,获得最新 ...
- java — 排序算法
1.冒泡排序 比较相邻元素,如果第一个比第二个大,就交换位置,每一次交换,当前 package BubbleSort; public class Test { public static void m ...
- 《剑指offer》---输出链表倒数第k个结点
本文算法使用python3实现 1 题目描述: 输入一个链表,输出该链表中倒数第k个结点. 时间限制:1s:空间限制:32768K 2 思路描述: 方法一:当链表长度为 $ n $ 时,输 ...