結果

問題 No.132 点と平面との距離
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-08-26 19:13:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,634 ms / 5,000 ms
コード長 2,702 bytes
コンパイル時間 611 ms
コンパイル使用メモリ 64,036 KB
実行使用メモリ 6,940 KB
最終ジャッジ日時 2024-04-25 18:02:43
合計ジャッジ時間 4,580 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
6,812 KB
testcase_01 AC 772 ms
6,940 KB
testcase_02 AC 2,634 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:93:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   93 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:95:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |     scanf("%lf %lf %lf", &p[0], &p[1], &p[2]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:100:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  100 |         scanf("%lf %lf %lf", &q[0], &q[1], &q[2]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <ciso646>
#include <cstdio>
#include <cstdlib>
#include <type_traits>
#include <valarray>
#include <vector>


constexpr int DIGITS = 12;


template <typename T> using RowVector = std::valarray<T>;
template <typename T> using Matrix = std::valarray<RowVector<T>>;


template <typename T> T determinant3(Matrix<T> a) {
    auto n = a.size();
    assert(n == 3 and a[0].size() == n);
    T d;
    d = a[0][0] * a[1][1] * a[2][2] + a[0][1] * a[1][2] * a[2][0];
    d += a[0][2] * a[1][0] * a[2][1] - a[0][0] * a[1][2] * a[2][1];
    d -= a[0][1] * a[1][0] * a[2][2] + a[0][2] * a[1][1] * a[2][0];
    return d;
}


template <typename T> T norm(RowVector<T> v) {
    static_assert(std::is_floating_point<T>() == true,
        "Use floating point numbers");
    auto u = v.apply([](T x) {return x * x; });
    return std::sqrt(u.sum());
}


template <typename T> RowVector<T> cross_product3(RowVector<T> u, RowVector<T> v) {
    assert(u.size() == 3 and v.size() == 3);
    auto m1 = u[1] * v[2] - u[2] * v[1];
    auto m2 = u[2] * v[0] - u[0] * v[2];
    auto m3 = u[0] * v[1] - u[1] * v[0];
    RowVector<T> cp = { m1, m2, m3 };
    return cp;
}


template <typename T> T volume_of_trigonal_pyramid(RowVector<T> p,
    RowVector<T> q1, RowVector<T> q2, RowVector<T> q3) {
    auto v1 = q1 - p;
    auto v2 = q2 - p;
    auto v3 = q3 - p;
    Matrix<T> a = { v1, v2, v3 };
    return std::abs(determinant3(a) / 6);
}


template <typename T> T area_of_triangle(RowVector<T> q1,
    RowVector<T> q2, RowVector<T> q3) {
    auto v2 = q2 - q1;
    auto v3 = q3 - q1;
    return norm<T>(cross_product3<T>(v2, v3)) / 2;
}


template <typename T> T dist(RowVector<T> p,
    RowVector<T> q1, RowVector<T> q2, RowVector<T> q3) {
    auto v = volume_of_trigonal_pyramid(p, q1, q2, q3);
    auto s = area_of_triangle(q1, q2, q3);
    return v * 3 / s;
}


template <typename T> T sum_of_dist(RowVector<T> p,
    std::vector<RowVector<T>> qs){
    auto n = qs.size();
    T d = 0;
    for (decltype(n) i = 0; i < n; i++)
    {
        for (decltype(n) j = i + 1; j < n; j++)
        {
            for (decltype(n) k = j + 1; k < n; k++)
            {
                d += dist(p, qs[i], qs[j], qs[k]);
            }
        }
    }
    return d;
}


int main() {
    int n;
    scanf("%d", &n);
    RowVector<double> p(3);
    scanf("%lf %lf %lf", &p[0], &p[1], &p[2]);
    std::vector<RowVector<double>> qs;
    for (decltype(n) i = 0; i < n; i++)
    {
        RowVector<double> q(3);
        scanf("%lf %lf %lf", &q[0], &q[1], &q[2]);
        qs.push_back(q);
    }
    auto ans = sum_of_dist(p, qs);
    printf("%.12lf\n", ans);
    return EXIT_SUCCESS;
}
0