思路:刚开始以为直接全排列枚举一下就好了,结果WA了。没想到没这么简单,重新排列后还要判断当前情况是否能够成立(这时可能会有相交的)。比如现在有两个大圆中间围着几个很小很小的圆,肯定是两个大圆先相切,中间几个小圆就有空隙了,这就能说通相邻的圆可以不相切而又使得box最小,解决方案是算出当前圆和之前的圆的相对位置取最大值,这样就能够保证既能够最大,又能够不相交
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x3fffffff
using namespace std;
double a[10], pos[10]; //a数组代表圆的半径,pos数组代表圆离原点的位置
void fun(int n) {
double x = 0;
for(int i = 0; i < n; i++) {
x = 2 * sqrt(a[i] * a[n]); //算出当前圆和之前的圆相切的水平距离
pos[n] = max(pos[n], x + pos[i]);//取最大的位置,这样才不会产生相交的圆
}
}
int main() {
int n, m;
scanf("%d", &n);
while(n--) {
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%lf", &a[i]);
}
sort(a, a + m);
double ans = INF, tmp;
do
{
memcpy(pos, a, sizeof(a));
for(int i = 1; i < m; i++) fun(i); //计算圆心实际可行的横坐标
tmp = 0;
for(int i = 0; i < m; i++) tmp = max(tmp, pos[i] + a[i]);//算出每个圆最右,取最大
ans = min(ans, tmp);
}while(next_permutation(a, a + m));
printf("%.3lf\n", ans);
}
return 0;
}