链接:https://ac.nowcoder.com/acm/contest/18458/C
来源:牛客网
时间限制:C/C++ 8秒,其他语言16秒
空间限制:C/C++ 1048576K,其他语言2097152K
Special Judge, 64bit IO Format: %lld
题目描述
There are n magical circles on a plane. They are centered at (x1, y1),(x2, y2), . . . ,(xn, yn), respectively. In the beginning, the radius of each circle is 0, and the radii of all magical circles will grow at the same rate. When an magical circle touches another, then it stops growing. Write a program to calculate the total area of all magical circles at the end of growing.
输入描述:
The first line contains an integer n to indicate the number of magical circles. The i-th of thefollowing n lines contains two space-separated integers xi and yi indicating that the i-th magicalcircle is centered at (xi , yi).
输出描述:
Output the total area of the circles.
示例1
输入
复制
4 0 0 1 0 1 1 0 1
输出
复制
3.14159265359
示例2
输入
复制
3 0 0 0 1 2 0
输出
复制
8.639379797371932
备注:
• 2 ≤ n ≤ 2000
• xi, yi ∈ [−109, 109] for i ∈ {1, 2, . . . , n}.
• All (xi , yi)’s are disinct points.
• A relative error of 10−6 is acceptable.
#include<bits/stdc++.h>
using namespace std;
int n, v[2345], cnt;
double x[2345], y[2345], r[2345], ans, d[2345][2345];
priority_queue<pair<double, int>>pq;
double dis(int i, int j)
{
return hypot(x[i]-x[j], y[i]-y[j]);
}
int main()
{
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> x[i] >> y[i];
r[i] = 9e9;
}
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
{
d[i][j] = d[j][i] = dis(i, j);
pq.push({-d[i][j]/2, i*3000+j});
}
while(cnt < n)
{
int i = pq.top().second / 3000, j = pq.top().second % 3000;
pq.pop();
if(!v[i] && !v[j])
{
r[i] = r[j] = d[i][j] / 2;
ans += 2 * r[i] * r[i];
v[i] = v[j] = 1;
cnt += 2;
}
else if(!v[i] || !v[j])
{
if(v[j])
swap(i, j);
r[j] = d[i][j] - r[i];
if(-r[j] < pq.top().first)
pq.push({-r[j], i*3000+j});
else
{
v[j] = 1;
ans += r[j] * r[j];
cnt++;
}
}
}
printf("%.8f\n", acos(-1)*ans);
}

该篇博客介绍了一个计算魔法圈在生长过程中总面积的算法问题。给定魔法圈的中心坐标和初始条件,利用优先队列求解当魔法圈相互接触时的最终面积。程序采用了C++语言实现,主要涉及几何计算、数据结构(优先队列)和搜索算法。
168万+

被折叠的 条评论
为什么被折叠?



