zoj 2419 || poj 2079 Triangle

211 篇文章 0 订阅
134 篇文章 1 订阅

代码有BUG

可以证明,面积最大的三角形的三点一定在凸包上。(自己证吧,恩)

好吧,我是看了大黄的这个题的题解才写出来的T T。。。

开始一直在想着怎么枚举这三个点,一般卡壳都是两个点之间的,本来想着会不会枚举凸包里所有的多边形。。然后。。。代价太大!

按照下面这个思路。。。A掉了 。。。开始以为必须不是纯净的凸包,但是卡壳的话必须是纯净的,我还弄了俩凸包,一个求纯净的一个求不纯净的,然后。。。XXXX。。。后来发现,解肯定存在在纯净凸包里面,因为如果不纯净的话,那么就可能有三点共线,那么不纯净里多的一定是中间的那个点,而面积的话,高相同的话一定是底最长面积最大。所以用纯净的就可以了。


“ 求点集中的最大三角形面积,O(n) 的旋转卡壳,先凸包,然后选取开头三个点 p,q,r 开始旋转,注意 r 不超过第一个点,q 不超过 r,p 不超过 q 。每次做三次推进,先推进 r,使 pq 不动面积最大,然后推进 q,再推进 p,如果三次都没有推进过,r 推进一格。每次推进完一个点都更新一下面积最大值。”


这个思路很显然啊,因为卡壳的原理就是,如果是找最远点对的话,那么最远距离一定是在极值的位置,就是一个有个峰值的。。。这个是三角形,也像爬坡,第一个点开始走,直到面积最大,说明在相同的底边,高已经到达最大了,然后改变底边的点也就是改变高,再求,更新最大值。。。

显然,因为每次都是爬到最大值才停下,也就是,每次改变都是固定一条边,找最大的高,又因为每次至少有一个点都只前进一步,所以每条边都枚举到了,而且距离它最远的高也找到了,那么最大三角形肯定也能找到。


啊。。通过这题,我对卡壳理解又多了点哈~~So magic~~

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

const int MAX = 50010;
const double eps = 1e-6;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
struct point{	double x,y;		};
point c[MAX],stk[MAX];
int top;
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
double disp2p(point a,point b) 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
bool cmp(point a,point b)  // 排序   
{  
    double len = crossProduct(c[0],a,b);  
    if( dd(len,0.0) )  
        return xy(disp2p(c[0],a),disp2p(c[0],b));  
    return xy(len,0.0);  
}
double area_triangle(point a,point b,point c)
{
	return fabs( crossProduct(a,b,c) )/2.0;
}
double max(double x,double y)
{
	return dy(x,y) ? x : y;
}
double RC(point *s,int n)
{
	int p,q,r;
	p = 0; q = 1; r = 2;
	double area = area_triangle(s[p],s[q],s[r]);
	while(1)
	{
		int pp = p,qq = q,rr = r;
		while( xy(fabs(crossProduct(s[p],s[q],s[r])),
				fabs(crossProduct(s[p],s[q],s[(r+1)%n]))))
		{
			area = max(area,area_triangle(s[p],s[q],s[(r+1)%n]));
			r = (r+1)%n;
		}
		while( xy(fabs(crossProduct(s[p],s[q],s[r])),
				fabs(crossProduct(s[p],s[(q+1)%n],s[r]))))
		{
			area = max(area,area_triangle(s[p],s[(q+1)%n],s[r]));
			q = (q+1)%n;
		}
		while( xy(fabs(crossProduct(s[p],s[q],s[r])),
				fabs(crossProduct(s[(p+1)%n],s[q],s[r]))))
		{
			area = max(area,area_triangle(s[(p+1)%n],s[q],s[r]));
			p = (p+1)%n;
		}
		if( pp == p && qq == q && rr == r )  // 如果一步都前进不了 
			r = (r+1)%n;
		if( r == 0 ) return area;
	}		
}

double Graham(int n)
{
    int tmp = 0;  
    for(int i=1; i<n; i++)
    	if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )
    		tmp = i;
    swap(c[0],c[tmp]);
    sort(c+1,c+n,cmp);
    stk[0] = c[0]; stk[1] = c[1];
    top = 1;
	for(int i=2; i<n; i++)
	{
		while( xyd( crossProduct(stk[top],stk[top-1],c[i]), 0.0 ) && top >= 1 )
			top--;
		stk[++top] = c[i];
	}
	return RC(stk,top+1);
}


int main()
{
	int n;
	
	while( ~scanf("%d",&n) && n != -1 )
	{
		for(int i=0; i<n; i++)
			scanf("%lf%lf",&c[i].x,&c[i].y);

		double ans = Graham(n);
		
		printf("%.2lf\n",ans);
	}

return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值