An overnight dance in discotheque (Codeforces 814D)
Tags: Greedy, geometry

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 ri. No 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 xi, yi 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 .

Note: The first sample corresponds to the illustrations in the legend.

Sample Inputs:
5
2 1 6
0 4 1
2 -1 3
1 -2 1
4 -1 1

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

Sample Outputs:
138.23007676

289.02652413

Solution (Python):
s = 0
t = [list(map(int, input().split())) for i in range(int(input()))]
f = lambda b: a[2] < b[2] and (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 <= (a[2] - b[2]) ** 2
for a in t:
    k = sum((f(b) for b in t))
    s += (-1, 1)[(k < 1) + k & 1] * a[2] ** 2
print(3.1415926536 * s)

Solution (Python):
s = 0
t = [list(map(int, input().split())) for i in range(int(input()))]
f = lambda b: a[2] < b[2] and (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 <= (a[2] - b[2]) ** 2
for a in t:
    k = sum((f(b) for b in t))
    s += (-1, 1)[(k < 1) + k & 1] * a[2] ** 2
print(3.1415926536 * s)

Solution (Python):
from collections import namedtuple
from math import hypot, pi

def contains(fst, scd):
    return hypot(fst.x - scd.x, fst.y - scd.y) < fst.r

def area(circle):
    return pi * circle.r ** 2

def find_prev(side, circle):
    for prev in reversed(side):
        if contains(prev, circle):
            return prev
Circle = namedtuple('Circle', 'x y r')
n = int(input())
cs = []
for i in range(n):
    cs.append(Circle(*map(int, input().split())))
cs = sorted(cs, key=lambda circle: -circle.r)
ans = 0.0
counts = dict()
left = []
right = []
for (ind, cur) in enumerate(cs):
    prev_left = find_prev(left, cur)
    prev_right = find_prev(right, cur)
    if prev_left is None:
        left.append(cur)
        counts[cur] = True
        ans += area(cur)
    elif prev_right is None:
        right.append(cur)
        counts[cur] = True
        ans += area(cur)
    elif counts[prev_left]:
        left.append(cur)
        counts[cur] = False
        ans -= area(cur)
    else:
        left.append(cur)
        counts[cur] = True
        ans += area(cur)
print(ans)