Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque(思维 贪心)

题意:有很多人跳舞,跳舞范围是个圆,然后跳舞的人分上半夜和下半夜,同一个圈圈里面的人不能同时跳。让你把所有能跳圈圈的面积加起来,得到的面积最大。

(所有圆之间只有包含和不想交关系。没有相交)


思路:因为没有相交的关系,所以我们可以把包含的一堆圆分别考虑,按半径大小排序,我们将最大的和次大的分别放上半夜和下

夜,然后第三大放上半夜挖去,第四大可以放在第三大挖去的圆中,第五大放下半夜挖去,第六大放第五大挖去的圆中.....这样就

找到规律了,对于一组包含的圆能形成的最大面积:ans = s1 + s2 - s3 + s4 - s5 + s6....(si表示第i层圆)

具体做法是对于每个圆,我们O(n)地算一遍它能包含哪些圆,然后把能被包含的圆的层数+1。

然后跑一遍所有圆,num为0的和num为奇数的面积都加上,为偶数的减去。


代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
const double pi = acos(-1.0);
struct node
{
    double x, y, r;
    int num;
    bool operator < (const node &a) const
    {
        if(r == a.r) return x == a.x ? y < a.y : x < a.x;
        return r > a.r;
    }
}a[maxn];
int n;

bool in(node a, node b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) < (a.r+b.r)*(a.r+b.r);
}

double cal(node a)
{
    return pi*a.r*a.r;
}

int main(void)
{
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++)
            scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].r), a[i].num = 0;
        sort(a+1, a+1+n);
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
                if(in(a[i], a[j]))
                    a[j].num++;
        double ans = 0.0;
        for(int i = 1; i <= n; i++)
        {
            if(!a[i].num || a[i].num%2) ans += cal(a[i]);
            else ans -= cal(a[i]);
        }
        printf("%.8f\n", ans);
    }
    return 0;
}

Examples
input
5
2 1 6
0 4 1
2 -1 3
1 -2 1
4 -1 1
output
138.23007676
input
8
0 0 1
0 0 2
0 0 3
0 0 4
0 0 5
0 0 6
0 0 7
0 0 8
output
289.02652413

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值