import sys
import math
from decimal import Decimal, getcontext
# 要设置高精度,需要使用decimal模块
getcontext().prec = 50
n, k = map(int, sys.stdin.readline().split())
# 使用字符串定义pi的所需精度
pi = Decimal("3.14159265358979324")
# 存储所有包含圆心的cost值
costs = []
for line in sys.stdin.readlines():
x, y, r = map(int, line.split())
x_dec, ydec, y_dec = Decimal(x), Decimal(y), Decimal(r)
# 计算移动长度
dist_sq = x_dec * x_dec + y_dec * y_dec
dist = dist_sq.sqrt()
length = r_dec - dist
# 如果length小于等于0,则不需要移动
if length <= 0:
continue
# 计算面积
s = pi * r_dec * r_dec
# 计算代价
cost = s * length
# 存入costs
costs.append(cost)
# 如果需要移动的个数小于等于给定个数,则不需要移动
if len(costs) <= k:
print(0)
else:
costs.sort()
circles_to_remove = len(costs) - k
print(sum(costs[:circles_to_remove]))
这精度真是见了鬼了,就不能直接用 math.pi 嘛
在此基础上进行优化的话就只能设计哈希表,使得存放时就有序了,不然每次都比较大小再插入排序大概还不如直接最后一次性排序,那不如直接简单点

京公网安备 11010502036488号