[CCPC2017 秦皇岛] ZOJ 3993 面积概率 + 计算几何(求两圆面积交)

CCPC训练赛第一场做到的题目,看了网上好多大佬的写法发现和我写的不是很相同,可能是我比较水,只能写比较暴力的做法。

题目描述

PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parachute onto an island and scavenge for weapons and equipment to kill others while avoiding getting killed themselves. BaoBao is a big fan of the game, but this time he is having some trouble selecting the safest building.
There are n buildings scattering on the island in the game, and we consider these buildings as points on a two-dimensional plane. At the beginning of each round, a circular safe area whose center is located at (0, 0) with radius R will be spawned on the island. After some time, the safe area will shrink down towards a random circle with radius r (r <= R). The whole new safe area is entirely contained in the original safe area (may be tangent to the original safe area), and the center of the new safe area is uniformly chosen within the original safe area.

题意

背景是吃鸡游戏的缩毒圈(玩过的应该比较好理解),初始的安全区是以(0,0)为圆心,R为半径的圆,在初始的安全区中会随机刷出一个半径为r的安全区,小安全区完全包含在大的之内。给出n个建筑物,保证每个建筑物都在初始的大安全区之中。如果在缩圈之后,建筑物还处在安全区中,则说这个建筑物是安全的。输出成为安全建筑物最大概率的建筑的个数与编号,如果有多个,按照编号号大小从小到大输出。

思路

对于任意一个安全区,在半径已知的条件下,只要知道它的圆心就可以唯一确定这个圆,而对于一个建筑物来说,能够覆盖到它的安全区的圆心离它的距离最大为r,只要满足圆心在这个范围之内,建筑物就是安全的。于是我们得到了第一个可行域。

而另一方面,后来的安全区域,必须包含在第一个安全区之内,也就是小圆要内切于大圆(圆心之间的距离 <= R - r )。从而得出,小圆的另一个可行区域,是以(0,0)为圆心,R - r为半径的圆。

所以说,一个建筑物安全的概率,是它安全的可行域面积与小圆的公共部分占小圆的可行域的多少。注意到,小圆的可行域是不变的,所以概率的大小就是两个可行域的面积交。

特别的要注意,R 等于 r,此时所以建筑物等概率(题目保证建筑物位于初始安全区内),直接输出1到n。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 103
const double eps = 1e-8;
const double PI = atan(1) * 4.0;
using namespace std;

struct node
{
    double s;
    int id;
}nodes[MAXN];
double dis(int x,int y)
{
    return sqrt(x * x + y * y);
}
double cal(double d,double r1,double r2)//求两圆的公共面积
{
    if(r1 + r2 < d + eps)//不相交
        return 0;
    if(d < fabs(r1 - r2) + eps)//包含
    {
        double r = min(r1,r2);
        return PI * r * r;
    }
    double x = (d * d + r1 * r1 - r2 * r2) / (2 * d);
    double t1 = acos(x / r1);
    double t2 = acos((d - x) / r2);
    return r1 * r1 * t1 + r2 * r2 * t2 - d * r1 * sin(t1);
}
bool cmp(node a,node b)
{
    if(fabs(a.s - b.s) < eps)
        return a.id < b.id;
    return a.s > b.s + eps;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,r,R;
        scanf("%d%d%d",&n,&R,&r);
        if(R == r)//特判相等的情况
        {
            int x,y;
            printf("%d\n",n);
            for(int i = 1;i < n;i++)
            {
                printf("%d ",i);
                scanf("%d%d",&x,&y);
            }
            scanf("%d%d",&x,&y);
            printf("%d\n",n);
            continue;
        }
        for(int i = 1;i <= n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            nodes[i].s = cal(dis(x,y),(double)(R - r),(double)(r));
            nodes[i].id = i;
            //printf("T%lf\n",nodes[i].s);
        }
        sort(nodes + 1,nodes + 1 + n,cmp);
        int ans = 1;
        for(int i = 2;i <= n;i++)
            if(nodes[i - 1].s - nodes[i].s < eps)
                ans++;
            else
                break;
        printf("%d\n",ans);
        for(int i = 1;i < ans;i++)
            printf("%d ",nodes[i].id);
        printf("%d\n",nodes[ans].id);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值