【HDU】4978 A simple probability problem. 凸包

题目分析:首先要求凸包是毫无疑问的,任意两点之间有边则如果内部有边与平行线相交则最外面一定有边与平行线相交。

重点是求概率。

由概率论书上什么抛针问题(我也不懂)可知一条长度为L的线段与间距为D的平行线相交的概率为2L / πD,由此我们可知凸包上一条线段Li与间距为D的平行线相交的概率为2Li / πD,又因为凸包上如果有一条线段与平行线相交则必定存在另一条线段Lj与Li同时与平行线相交(凸包是封闭凸多边形),那么2Li / πD = ∑Pij(Pij表示Li与Lj同时与平行线相交的概率,同时i!=j),则凸包与平行线相交的概率为1 / 2 * sum{ sum{ Pij | i != j } | 1 <= i <= n },除以2的原因是Pij与Pji是等价的,是同时满足的。

那么问题最后就转化成了求凸包的周长了。这个就很简单了,也就不说了。


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define CPY( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 100 ;

struct Point {
	double x , y ;
	Point () {}
	Point ( double x , double y ) : x ( x ) , y ( y ) {}
	Point operator - ( const Point& a ) const {
		return Point ( x - a.x , y - a.y ) ;
	}
	bool operator < ( const Point& a ) const {
		return x != a.x ? x < a.x : y < a.y ;
	}
	void input () {
		scanf ( "%lf%lf" , &x , &y ) ;
	}
} p[MAXN] ;

int n , D ;
int S[MAXN] , top ;

double cross ( const Point& a , const Point& b ) {
	return a.x * b.y - a.y * b.x ;
}

double dist ( const Point& a ) {
	return sqrt ( a.x * a.x + a.y * a.y ) ;
}

void convex_hull ( Point p[] , int n ) {
	top = 0 ;
	sort ( p , p + n ) ;
	REP ( i , 0 , n ) {
		while ( top > 1 && cross ( p[i] - p[S[top - 2]] , p[S[top - 1]] - p[S[top - 2]] ) >= 0 ) -- top ;
		S[top ++] = i ;
	}
	int k = top ;
	REV ( i , n - 2 , 0 ) {
		while ( top > k && cross ( p[i] - p[S[top - 2]] , p[S[top - 1]] - p[S[top - 2]] ) >= 0 ) -- top ;
		S[top ++] = i ;
	}
}

void solve () {
	double L = 0 ;
	scanf ( "%d%d" , &n , &D ) ;
	REP ( i , 0 , n ) p[i].input () ;
	convex_hull ( p , n ) ;
	REP ( i , 1 , top ) L += dist ( p[S[i]] - p[S[i - 1]] ) ;
	printf ( "%.4f\n" , L / acos ( -1.0 ) / D ) ;
}

int main () {
	int T , cas = 0 ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) {
		printf ( "Case #%d: " , ++ cas ) ;
		solve () ;
	}
	return 0 ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值