CodeForces 814D An overnight dance in discotheque(贪心+dfs)

The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?

The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area Ci described by a center (xi, yi) and a radius riNo two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges Ci and Cj are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges.

Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time.

But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above.

By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum.

Input

The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers.

The following n lines each describes a dancer: the i-th line among them contains three space-separated integers xiyi and ri( - 106 ≤ xi, yi ≤ 106, 1 ≤ ri ≤ 106), describing a circular movement range centered at (xi, yi) with radius ri.

Output

Output one decimal number — the largest achievable sum of spaciousness over two halves of the night.

The output is considered correct if it has a relative or absolute error of at most 10 - 9. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

Examples
input
Copy
5
2 1 6
0 4 1
2 -1 3
1 -2 1
4 -1 1
output
Copy
138.23007676
input
Copy
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
Copy
289.02652413
Note

The first sample corresponds to the illustrations in the legend.

题意:

平面直角坐标系中有n个圆,每两个圆之间最多只有一个交点.

要求你把圆分成两份, 每一份的圆中加上被覆盖偶数次的圆的面积,减去被覆盖奇数次的圆的面积,求两份加起来最大的面积和.

                                                                                                                                                     ----translate by 神仙gmy

题解:

首先显然这些圆的包含关系是一棵树

现在就是要求在树上选取一些点相加再减去剩下点,使这棵树的贡献最大。

然后推一波贪心思路

首先要清楚的是一个圆的面积比他所有儿子节点面积之和还要大。

所以肯定优先选取两个平面放根节点和根节点的子节点

这样子的话孙子节点无论如何都只能减掉

此时来看第四代,要么选,减掉第五代,要么不选,加上第五代,结合上面的结论,肯定选第四代比较划算。

所以以此类推,一遍dfs就能跑出正解

代码如下:

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;

double pi=acos(-1);
struct node
{
    int x,y,r;
    int d1,d2,d3,d4;  //d1 ue d2 shita d3 hidari d4 migi
    double s;
} c[1010];

double ans=0.0;
vector<int> g[1010];
int vis[1010];

int check(node a,node b)
{
    if(a.d1<=b.d1&&a.d2>=b.d2&&a.d3>=b.d3&&a.d4<=b.d4)
    {
        return 1;
    }
    return 0;
}

int cmp(node a,node b)
{
    return a.r<b.r;
}

int n;

double dfs(int now,int f,int deep)
{
    vis[now]=1;
    double tmp=0.0;
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f) continue;
        tmp+=dfs(g[now][i],now,deep+1);
    }
    if(f==0) return tmp;
    return tmp+((deep&1)?c[now].s:-c[now].s);
}

signed main()
{
    scanf("%lld",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld%lld%lld",&c[i].x,&c[i].y,&c[i].r);
        c[i].d1=c[i].y+c[i].r;
        c[i].d2=c[i].y-c[i].r;
        c[i].d3=c[i].x-c[i].r;
        c[i].d4=c[i].x+c[i].r;
        c[i].s=c[i].r*c[i].r*pi;
    }
    sort(c+1,c+n+1,cmp);
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++)
        {
            if(check(c[i],c[j]))
            {
                g[i].push_back(j);
                g[j].push_back(i);
                break;
            }
        }
    }
    for(int i=n;i>=1;i--)
    {
        if(!vis[i])
        {
            ans+=dfs(i,0,0)+c[i].s;
        }
    }
    printf("%.9lf\n",ans);
}

猜你喜欢

转载自www.cnblogs.com/stxy-ferryman/p/9549135.html