HDU 4978 A simple probability problem 【凸包】【几何】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4978

题目大意:给你在平面上一些平行的线,线之间的距离是D,然后在给你一些不共线的点,每两点之间都有线相连,现在问这些点连成的线与平面上平行的线相交的概率。

这里要先介绍一个模型:蒲丰投针问题

知道了这个模型之后我们就能解决只有两个点的问题了,P=2L/πD (L为针长,D为平行线间距)

但是给出的是有很多个点,所以我们还要知道蒲丰投针问题扩展,将给出的点构造成一个凸包,若是凸包内部的线可以和平面上平行的线相交,则凸包一定可以和平面上平行的线相交。此时P
= C/πD (C为凸包周长)。

做题太少了.......

// CONVEX HULL I
// modified by rr 不能去掉点集中重合的点
#include<iostream>
#include<math.h>
#include<cstring>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*
传统意义	修正写法
a == b	    fabs(a – b) < eps
a != b	    fabs(a – b) > eps
a < b	    a – b < -eps
a <= b	    a – b < eps
a > b	    a – b > eps
a >= b	    a – b > -eps
*/
#define offset 100
#define eps 1e-8
#define PI acos(-1.0)
#define MAXN 105
#define zero(x) (((x)>0? (x):-(x))<eps) //是0则返回1,否则返回0
#define _sign(x) ((x)>eps? 1:((x)<-eps? 2:0))//负数 0 正数 分别返回 2 0 1

struct point
{
    double x;
    double y;
    point(const double &x = 0, const double &y = 0):x(x), y(y) {}
    void in()
    {
        scanf("%lf %lf", &x, &y);
    }
    void out()const
    {
        printf("%.2f %.2f\n",x, y);
    }
};

//计算叉乘
double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

double dis(point a, point b)
{
    return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) ;
}

//graham算法顺时针构造包含所有共线点的凸包,O(nlogn)
point p1,p2;
int graham_cp(const void* a,const void* b)
{
    double ret=xmult(*((point*)a),*((point*)b),p1);
    return zero(ret)?(xmult(*((point*)a),*((point*)b),p2)>0?1:-1):(ret>0?1:-1);
}
void _graham(int n,point* p,int& s,point* ch)
{
    int i,k=0;
    for (p1=p2=p[0],i=1; i<n; p2.x+=p[i].x,p2.y+=p[i].y,i++)
        if (p1.y-p[i].y>eps||(zero(p1.y-p[i].y)&&p1.x>p[i].x))
            p1=p[k=i];
    p2.x/=n,p2.y/=n;
    p[k]=p[0],p[0]=p1;
    qsort(p+1,n-1,sizeof(point),graham_cp);
    for (ch[0]=p[0],ch[1]=p[1],ch[2]=p[2],s=i=3; i<n; ch[s++]=p[i++])
        for (; s>2&&xmult(ch[s-2],p[i],ch[s-1])<-eps; s--);
}

//构造凸包接口函数,传入原始点集大小n,点集p(p原有顺序被打乱!)
//返回凸包大小,凸包的点在convex中
//参数maxsize为1包含共线点,为0不包含共线点,缺省为1
//参数clockwise为1顺时针构造,为0逆时针构造,缺省为1
//在输入仅有若干共线点时算法不稳定,可能有此类情况请另行处理!
//不能去掉点集中重合的点
int graham(int n,point* p,point* convex,int maxsize=1,int dir=1)  //n必须大于3
{
    point* temp=new point[n];
    int s,i;
    _graham(n,p,s,temp);
    for (convex[0]=temp[0],n=1,i=(dir?1:(s-1)); dir?(i<s):i; i+=(dir?1:-1))
        if (maxsize||!zero(xmult(temp[i-1],temp[i],temp[(i+1)%s])))
            convex[n++]=temp[i];
    delete []temp;
    return n;
}

int main()
{
    int T, N, D, cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        point P[MAXN];
        point c[MAXN];
        scanf("%d%d",&N,&D);
        for(int i = 0; i < N; i ++)
            P[i].in();
        double ans = 0 ;

        if(N>2)
        {
            int num = graham(N,P,c);
            for(int i = 0; i < num-1; i ++)
            {
                ans+=dis(c[i],c[i+1]);
            }
            ans+=dis(c[0],c[num-1]);
        }
        else ans = dis(P[0],P[1])*2;

        printf("Case #%d: %.4lf\n",cas++,ans/(PI*D));
    }
}
时间: 2024-09-29 10:52:40

HDU 4978 A simple probability problem 【凸包】【几何】的相关文章

HDU 4978 A simple probability problem

A simple probability problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 43    Accepted Submission(s): 14 Problem Description Equally-spaced parallel lines lie on an infinite plane. The sep

HDU 4978 A simple probability problem.(概率模型+凸包周长)

题意:一个直径为d的圆中有n个点,每两点间有线段连接,一个平面上有间距都为d的平行线,求将原放在该平面上至少有一条线段与平行线相交的概率: 思路: 蒲丰针问题:http://wenku.baidu.com/link?url=s3rJRGUhCZ7kmsXA6o7Edr8h1rJJbibu2Ocs1Yf5BpsPwSkjkK9w-uVSV4d-cBGV36UA9bpxVfqLLA9qlPwbWkYbjkFzDaP_N5dtWHVT_mi 长度为L的针与间距为d的平行线相交的概率为P=2*L/(pi

hdu4987A simple probability problem.(凸包)

链接 多校的最后一场,当时没看懂题意,看题目还以为是概率问题就没深看. 官方题解 对于他说的第一种,考虑长为L的线段 概率为2L/(pi*d), 可以理解,下面的就不知道在说啥了.. 按我初始的想法想要枚举角度,根据凸包的高度差得出概率,不过有一种更简便的方式,就是题解中的求出凸包的周长,这种方式我的理解为,凸包中的一条线段穿过直线的概率就跟上面所说的线段一样2L/(pi*d),不过因为他是个多边形,有进就肯定有出,所以穿过一条线段就相当于穿过两条,整体来说就是/2...不知道这种理解方式对不对

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

HDU 4974 A simple water problem(贪心)

HDU 4974 A simple water problem 题目链接 签到题,很容易贪心得到答案是(sum + 1) / 2和ai最大值的最大值 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; typedef long long ll; int t, n; ll a, Max, sum; int main(

HDU - 4974 A simple water problem

Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or 1 for each competitor and add it to the total score

2014多校第十场1004 || HDU 4974 A simple water problem

题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思路 : ans = max(maxx,(sum+1)/2) :其实想想就可以,如果所有得分中最大值没有和的一半大,那就是队伍中一半一半对打,否则的话最大的那个就都包了. 1 #include <cstdio> 2 #include <cstring> 3 #include <st

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 4974 A simple water problem 模拟(水

水题. #include <cstdio> #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long ll; priority_queue<int> q; int main() { int T, cas = 0; scanf("%d", &T); while(T-- > 0) {