码迷,mamicode.com
首页 > Web开发 > 详细

luogu4035 [JSOI2008]球形空间产生器

时间:2018-03-06 23:09:10      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:cin   ace   abs   str   source   clu   cst   post   pac   

如果单按照距离相等的话既是高次也没有半径,所以因为给了 \(n+1\) 组点就想到两两做差。
假如一组点是 \(\{a_i\}\) 一组是 \(\{b_i\}\),我们能轻易地得出
\[\sum_{i=1}^n(x_i-a_i)^2=\sum_{i=1}^n(x_i-b_i)^2 \Rightarrow \sum_{i=1}^n 2(b_i-a_i)x_i=\sum_{i=1}^n(b_i^2-a_i^2)\]
这是一个 \(n\) 元一次线性方程。我们能搞出 \(n\) 组这样的方程,高斯消元解之。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
double tmp1[15], tmp2[15], a[15][15];
void gauss(){
    for(int i=1; i<=n; i++){
        int maxi=i;
        for(int j=i+1; j<=n; j++)
            if(fabs(a[j][i])>fabs(a[i][i]))
                maxi = j;
        double now=a[maxi][i];
        swap(a[maxi], a[i]);
        for(int j=i; j<=n+1; j++)
            a[i][j] /= now;
        for(int j=i+1; j<=n; j++){
            now = a[j][i];
            for(int k=i; k<=n+1; k++)
                a[j][k] -= a[i][k] * now;
        }
    }
    for(int i=n; i>=1; i--)
        for(int j=1; j<=i-1; j++){
            a[j][n+1] -= a[j][i] * a[i][n+1];
            a[j][i] = 0;
        }
}
int main(){
    cin>>n;
    for(int i=1; i<=n; i++) scanf("%lf", &tmp1[i]);
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++)
            scanf("%lf", &tmp2[j]);
        for(int j=1; j<=n; j++){
            a[i][j] = 2 * (tmp2[j] - tmp1[j]);
            a[i][n+1] += tmp2[j] * tmp2[j] - tmp1[j] * tmp1[j];
        }
        swap(tmp1, tmp2);
    }
    gauss();
    for(int i=1; i<=n; i++)
        printf("%.3f ", a[i][n+1]);
    printf("\n");
    return 0;
}

luogu4035 [JSOI2008]球形空间产生器

标签:cin   ace   abs   str   source   clu   cst   post   pac   

原文地址:https://www.cnblogs.com/poorpool/p/8519371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!