题意:有很多人跳舞,跳舞范围是个圆,然后跳舞的人分上半夜和下半夜,同一个圈圈里面的人不能同时跳。让你把所有能跳圈圈的面积加起来,得到的面积最大。
(所有圆之间只有包含和不想交关系。没有相交)
思路:因为没有相交的关系,所以我们可以把包含的一堆圆分别考虑,按半径大小排序,我们将最大的和次大的分别放上半夜和下
半夜,然后第三大放上半夜挖去,第四大可以放在第三大挖去的圆中,第五大放下半夜挖去,第六大放第五大挖去的圆中.....这样就
找到规律了,对于一组包含的圆能形成的最大面积: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