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*d);

凸N边形与间距为d的平行线相交的概率为P=C/(pi*d);(C为凸N边形周长);

本题需要求的即凸包周长,结果为周长除以(π乘以直径);

样例跑出来不一样,几何题就是这么神奇。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double epsi=1e-10;
const double pi=acos(-1.0);
const int maxn=10005;
struct point{
    double x,y;
    point(){}
    point(double xx,double yy):x(xx),y(yy){}
    point operator -(const point &op2)const{
        return point(x-op2.x,y-op2.y);
    }
    double operator ^(const point &op2) const{ //两个点向量的叉积
        return x*op2.y-y*op2.x;
    }
};
inline int sign(const double &x){
    if(x>epsi) return 1;
    if(x<-epsi) return -1;
    return 0;
}
inline double sqr(const double &x){
    return x*x;
}
inline double mul(const point &p0,const point &p1,const point &p2){
    return (p1-p0)^(p2-p0);
}
inline double dis2(const point &p0,const point &p1){
    return sqr(p0.x-p1.x)+sqr(p0.y-p1.y);
}
inline double dis(const point &p0,const point &p1){
    return sqrt(dis2(p0,p1));
}
int n,l;        //n个顶点,最近距离为l
point p[maxn],convex_hull;  //多边形顶点序列为p[],最低位置的点为convex_hull
inline bool cmp(const point &a,const point &b){ //相对最低点,各点极角从小到大,距离从近到远排序
    return sign(mul(convex_hull,a,b))>0||sign(mul(convex_hull,a,b))==0&&dis2(convex_hull,a)<dis2(convex_hull,b);
}
int convex(point *a,int n,point *b){ //计算含n个点的点集a的凸包b
    if(n<3) printf("Wrong in Line %d\n",__LINE__); //顶点数小于3,输出失败信息
    for(int i=1;i<n;i++)                          //计算最低点convex_hull
        if(sign(a[i].x-a[0].x)<0||sign(a[i].x-a[0].x)==0&&sign(a[i].y-a[0].y)<0)
        swap(a[0],a[i]);
    convex_hull=a[0];
    sort(a,a+n,cmp);                      //按极角和距离排序
    int newn=2;
    b[0]=a[0];b[1]=a[1];                //a[0],a[1]入栈
    for(int i=2;i<n;i++){
        while(newn>1&&sign(mul(b[newn-1],b[newn-2],a[i]))>=0) --newn; //弹出栈顶所有未左转指向扫描顶点i的元素
        b[newn++]=a[i];                   //顶点i入栈
    }
    return newn;           //栈顶指针
}
int main(){
    int t,cas;
    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d",&n,&l);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        printf("Case #%d: ",cas);
        if(n>2)
        {
            n=convex(p,n,p);
            p[n]=p[0]; //首尾相连
            double ans=0;
            for(int i=0;i<n;i++)
                ans+=dis(p[i],p[i+1]);  //累计凸包边长
            //ans+=2*pi*l;
            printf("%.4lf\n",ans/(pi*l));
        }
        else if(n==2)
        {
            double ans=dis(p[0],p[1]);
            printf("%.4lf\n",2*ans/(pi*l));
        }
        else
            printf("0.0000\n");

    }
    return 0;
}
时间: 2024-11-05 19:39:12

HDU 4978 A simple probability problem.(概率模型+凸包周长)的相关文章

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4978 题目大意:给你在平面上一些平行的线,线之间的距离是D,然后在给你一些不共线的点,每两点之间都有线相连,现在问这些点连成的线与平面上平行的线相交的概率. 这里要先介绍一个模型:蒲丰投针问题 知道了这个模型之后我们就能解决只有两个点的问题了,P=2L/πD (L为针长,D为平行线间距) 但是给出的是有很多个点,所以我们还要知道蒲丰投针问题扩展,将给出的点构造成一个凸包,若是凸包内部的线可以和平面上

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 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) {

矩阵十题【八】 HDU 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: If x < 10   ,则  f(x) = x. If x >= 10 ,则  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题