思路比较烂,直接上全排列。用了2s。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#define ll long long
#define INF 2139062143
#define inf -2139062144
#define MOD 20071027
#define MAXN 1000005
using namespace std;
int w[8][2]= {{1,1},{0,1},{1,-1},{-1,0},{-1,-1},{0,-1},{-1,1},{1,0}};
int main()
{
int h[10];
while(scanf("%d%d%d%d%d%d%d%d",&h[0],&h[1],&h[2],&h[3],&h[4],&h[5],&h[6],&h[7]))
{
if(!h[0]&&!h[1]&&!h[2]&&!h[3]&&!h[4]&&!h[5]&&!h[6]&&!h[7]) break;
sort(h,h+8);
double ans=0;
bool fir=false;
do
{
double res=0,a=0,b=0;
for(int i=0; i<8; ++i)
{
a+=h[i]*w[i][0];
b+=h[i]*w[i][1];
}
res=sqrt(a*a+b*b);
if(fir==false)
{
fir=true;
ans=res;
}
else ans=min(res,ans);
}
while(next_permutation(h,h+8));
printf("%.3lf\n",ans);
}
return 0;
}