二维凸包求助
查看原帖
二维凸包求助
700210
OIer_Tan楼主2024/7/19 16:48

RT,思路大概是因为在凸壳上的点总有一种方式旋转点集,使横坐标是最小或最大。

现在的问题是,下了数据之后发现后几个点差的都并不多,但是总是不对。感觉像是精度的问题。

有没有大佬可以帮忙看看?

#include <bits/stdc++.h>

#ifndef CRT
#define endl '\n'
#endif

using namespace std ;

typedef long long ll ;
typedef unsigned long long ull ;
typedef long double ld ;

const ll N = 1e5 + 5 ;

struct point 
{
	ld x , y ;
	friend bool operator < ( const point & a , const point & b )
	{
		if ( a.x == b.x )
		{
			return a.y < b.y ;
		}
		return a.x < b.x ;
	}
	friend bool operator != ( const point & a , const point & b )
	{
		return a.x != b.x || a.y != b.y ;
	}
} p [N] ; 

ll n ;

const ld Cos = cosl ( 1.0l / 180.0l * acosl ( -1 ) ) ,
		 Sin = sinl ( 1.0l / 180.0l * acosl ( -1 ) ) ;

point rotate ( const point & p )
{
	point t ;
	t.x = p.x * Cos + p.y * Sin ;
	t.y = p.y * Cos - p.x * Sin ;
	return t ;
}

ld dis ( point a , point b )
{
	return sqrtl ( pow ( a.x - b.x , 2 ) + pow ( a.y - b.y , 2 ) ) ;
}

int main ()
{
#ifndef ONLINE_JUDGE
	freopen ( "P2742_8.in" , "r" , stdin ) ;
	freopen ( ".out" , "w" , stdout ) ;
#endif
	ios::sync_with_stdio ( 0 ) ;
	cin.tie ( 0 ) ;
	cout.tie ( 0 ) ;
	cin >> n ;
	for ( ll i = 1 ; i <= n ; i ++ )
	{
		cin >> p [i].x >> p [i].y ;
	}
	vector <point> uphull , dnhull ;
	sort ( p + 1 , p + 1 + n ) ;
	point tmp [N] ;
	copy ( p + 1 , p + 1 + n , tmp + 1 ) ;
	for ( ll _ = 0 ; _ <= 180 ; _ ++ )
	{
		ll minx = 1 , maxx = 1 ;
		for ( ll i = 1 ; i <= n ; i ++ )
		{
			if ( p [minx].x > p [i].x )
			{
				minx = i ;
			}
			if ( p [maxx].x <= p [i].x )
			{
				maxx = i ;
			}
		}
		if ( uphull.empty () || uphull.back() != tmp [maxx] )
		{
			uphull.emplace_back ( tmp [maxx] ) ;
		}
		if ( dnhull.empty () || dnhull.back() != tmp [minx] )
		{
			dnhull.emplace_back ( tmp [minx] ) ;
		}
		for ( ll i = 1 ; i <= n ; i ++ )
		{
			p [i] = rotate ( p [i] ) ;
		}
	}
	uphull.emplace_back ( dnhull.front () ) ;
	dnhull.emplace_back ( uphull.front () ) ;
	vector <point> hull ;
	copy ( dnhull.begin () , dnhull.end () , back_insert_iterator ( hull ) ) ;
	copy ( uphull.begin () , uphull.end () , back_insert_iterator ( hull ) ) ;
	ld ans = 0 ;
	for ( ll i = 1 ; i < hull.size () ; i ++ )
	{
		ans += dis ( hull [i] , hull [i - 1] ) ;
	}
	printf ( "%.2Lf" , ans ) ;
	return 0 ;
}
2024/7/19 16:48
加载中...