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 separation between adjacent lines is D (D>0). Now considering a circle of diameter D. N points lie on or in the circle. It is guaranteed that any three points are not collinear. Between any two points there's a needle. Find the possibility that, if the circle is randomly (with equal probability on any position and direction) thrown onto the same plane described above (with the equally-spaced parallel lines of separation d), at least one needle crosses a line.
 

Input
The first line contains a single integer T (1 <= T <= 100), the number of test cases.

For each set of input data, the first line gives two integers, N and D (N<=100), as described above. You can consider the center of the circle is default as the origin. Lastly N lines is followed, each containing two real numbers that representing the coordinate of a point lying within the circle.
 

Output
Each output should occupy one line. Each line should start with "Case #i: ", followed by a real number round to four decimal places, the probability that at least one needle crosses one line.
 

Sample Input
 
 
2 2 2 -0.5 0 0.5 0 3 3 0 1 1 0 -1 0
 

Sample Output
 
 
Case #1: 0.3183 Case #2: 0.5123
 

Source
2014 Multi-University Training Contest 10


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


题目大意  :一个无限大的平面上有无数条等距平行线,每两条间距为D,给一个直径为D的圆,有n个点分布在圆上或者圆内,点的输入是依照已圆心为原点的坐标系,规定随意三点不共线。随意两点间的线段记为一根针。如今问将该圆投到平面上至少有一根针和当中一条平行线相交的概率


题目分析  :计算几何的问题,首先考虑仅仅有两点的情况即一根针。这就是一个布丰投针问题,公式为P=2L/πD (L为针长。D为平行线间距)。再考虑多个点,显然是个凸包问题,假设凸包边上的线能够与平行线相交,凸包内的线必定能够与平行线相交,由投针问题的推广我们能够得到公式P = C/πD (C为凸包周长),详见布丰投针及推广


#include <cstdio>
#include <cstdlib>
#include <cmath>
#define N 200
#define inf 1e-6
#define PI 3.141592653
typedef struct
{
    double x;
    double y;
}point;
point points[N]; 
point chs[N];    
int sp; 

//求凸包周长的模板
double dis(point a, point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) * 1.0 + (a.y - b.y) * (a.y - b.y));
}


double multi(point p0, point p1, point p2)
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
int cmp(const void *p, const void *q)
{
     point a = *(point *)p;
     point b = *(point *)q;
    double k = multi(points[0], a, b);
    if(k < -inf)
        return 1;
    else if(fabs(k) < inf && (dis(a, points[0]) - dis(b, points[0])) > inf) 
        return 1;
    else return -1;
}
void convex_hull(int n)
{
    int k, d;
    double miny = points[0].y;
    int index = 0;
    for(int i = 1; i < n; i++) 
    {
        if(points[i].y < miny) 
        {
            miny = points[i].y;
            index = i;
        }
        else if(points[i].y == miny && points[i].x < points[index].x) 
             index = i;
    }
    point temp;
    temp = points[index];
    points[index] = points[0];
    points[0] = temp;
    qsort(points+1, n-1, sizeof(points[0]), cmp);
    chs[0] = points[n-1];
    chs[1] = points[0];
    sp = 1;
    k = 1;
    while(k <= n-1)
    {
        double d = multi(chs[sp], chs[sp-1], points[k]);
        if(d <= 0)
        {
             sp++;
             chs[sp] = points[k];
             k++;
        }
        else sp--;
    }
}
int main()
{
    double sum, d;
    int T, n;
    scanf("%d",&T);
    for(int Ca = 1; Ca <= T; Ca++)
    {
        sum = 0;
        scanf("%d %lf", &n, &d);
        if(n == 0 || n == 1)
        {
            printf("Case #%d: 0.0000\n", Ca);
            continue;
        }
        for(int i = 0; i < n; i++)
            scanf("%lf%lf", &points[i].x, &points[i].y);
        if(n == 2)
        {
            double len = dis(points[0],points[1]);
            printf("Case #%d: %.4f\n", Ca, (2 * len) / (PI * d));
            continue;
        }
        convex_hull(n);
        for(int i = 1; i <= sp; i++) 
            sum += dis(chs[i-1], chs[i]);
        sum += dis(chs[0], chs[sp]);   //算出凸包周长
        printf("Case #%d: %.4f\n", Ca, sum / (PI * d));
    }
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5234393.html,如需转载请自行联系原作者

相关文章
|
机器学习/深度学习 人工智能 Apache
AI工具包
AI现在已经成为当下最流行的领域,作者通过分析其技术的支持,整理了AI工具包,编者也顺带向读者推荐了阿里云相关的学习资料,以方便大家学习。
3880 0
|
5天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
16天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1315 5
|
2天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
15天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1367 87
|
2天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
4天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
200 82
2025年阿里云域名备案流程(新手图文详细流程)