結果

問題 No.2438 Double Least Square
ユーザー ebi_flyebi_fly
提出日時 2023-08-13 02:53:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,467 bytes
コンパイル時間 8,287 ms
コンパイル使用メモリ 422,164 KB
実行使用メモリ 13,768 KB
最終ジャッジ日時 2024-11-26 23:04:45
合計ジャッジ時間 58,925 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,768 KB
testcase_01 AC 2 ms
10,916 KB
testcase_02 AC 2 ms
13,764 KB
testcase_03 AC 2 ms
10,784 KB
testcase_04 AC 1 ms
13,764 KB
testcase_05 AC 2 ms
11,040 KB
testcase_06 AC 2 ms
13,764 KB
testcase_07 AC 1 ms
10,788 KB
testcase_08 AC 2 ms
13,764 KB
testcase_09 AC 3 ms
10,916 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 911 ms
10,912 KB
testcase_16 AC 878 ms
13,768 KB
testcase_17 AC 878 ms
10,912 KB
testcase_18 AC 885 ms
13,768 KB
testcase_19 AC 868 ms
10,912 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 3 ms
11,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <boost/multiprecision/cpp_int.hpp>

#define rep(i, s, n) for (int i = int(s); i < int(n); i++)

#define all(v) (v).begin(), (v).end()

using boost::multiprecision::cpp_int;
using bigint = cpp_int;

template <class T> void arg_sort_ll(std::vector<std::pair<T, T>>& a) {
    using Point = std::pair<T, T>;
    int n = (int)a.size();
    std::vector ps(4, std::vector<Point>());
    auto idx = [](Point v) -> int {
        if (v.second >= 0)
            return (v.first >= 0) ? 0 : 1;
        else
            return (v.first >= 0) ? 3 : 2;
    };
    for (auto p : a) {
        assert(!(p.first == 0 && p.second == 0));
        ps[idx(p)].emplace_back(p);
    }
    a.clear();
    a.reserve(n);
    for (int j = 0; j < 4; j++) {
        int i = (3 + j) % 4;
        std::sort(ps[i].begin(), ps[i].end(), [](Point& p1, Point& p2) -> bool {
            T flag = p1.first * p2.second - p2.first * p1.second;
            return flag == 0 ? (p1.first * p1.first + p1.second * p1.second <
                                p2.first * p2.first + p2.second * p2.second)
                             : flag > 0;
        });
        for (auto& p : ps[i]) a.emplace_back(p);
    }
    return;
}

using ld = long double;

bigint GCD(bigint a, bigint b) {
    if (b == 0)
        return a;
    else
        return GCD(b, a % b);
}

struct rational {
    rational() : p(0), q(1) {}
    rational(bigint n) : p(n), q(1) {}
    rational(bigint n, bigint m) {
        assert(m != 0);
        if (m < 0) n = -n, m = -m;
        bigint g = GCD(n, m);
        p = n / g;
        q = m / g;
    }
    explicit operator const ld() const {
        return ld(p) / ld(q);
    }
    rational& operator+=(const rational& rhs) {
        bigint g = GCD(q, rhs.q);
        bigint np = rhs.q / g * p + q / g * rhs.p;
        bigint nq = q / g * rhs.q;
        bigint ng = GCD(np, nq);
        p = np / ng, q = nq / ng;
        return *this;
    }
    rational& operator-=(const rational& rhs) {
        (*this) += rational(-rhs.p, rhs.q);
        return *this;
    }
    rational& operator*=(const rational& rhs) {
        bigint g1 = GCD(q, rhs.p), g2 = GCD(p, rhs.q);
        bigint np = p / g2 * rhs.p / g1;
        bigint nq = q / g1 * rhs.q / g2;
        p = np, q = nq;
        return *this;
    }
    rational& operator/=(const rational& rhs) {
        (*this) *= rational(rhs.q, rhs.p);
        return *this;
    }
    rational operator+() const {
        return *this;
    }
    rational operator-() const {
        return rational() - *this;
    }
    friend rational operator+(const rational& lhs, const rational& rhs) {
        return rational(lhs) += rhs;
    }
    friend rational operator-(const rational& lhs, const rational& rhs) {
        return rational(lhs) -= rhs;
    }
    friend rational operator*(const rational& lhs, const rational& rhs) {
        return rational(lhs) *= rhs;
    }
    friend rational operator/(const rational& lhs, const rational& rhs) {
        return rational(lhs) /= rhs;
    }
    friend bool operator==(const rational& lhs, const rational& rhs) {
        return lhs.p == rhs.p && lhs.q == rhs.q;
    }
    friend bool operator!=(const rational& lhs, const rational& rhs) {
        return lhs.p != rhs.p || lhs.q != rhs.q;
    }
    friend bool operator<(const rational lhs, const rational rhs) {
        return less_than(lhs, rhs);
    }
    friend bool operator>(const rational lhs, const rational rhs) {
        return less_than(rhs, lhs);
    }
    friend bool operator<=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs < rhs;
    }
    friend bool operator>=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs > rhs;
    }
    friend std::ostream& operator<<(std::ostream& os, const rational& r) {
        return os << r.p << " / " << r.q;
    }
    std::pair<bigint, bigint> val() const {
        return {p, q};
    }

  private:
    bigint p, q;
    static bool less_than(rational lhs, rational rhs) {
        bigint lv = bigint(lhs.p) * bigint(rhs.q);
        bigint rv = bigint(lhs.q) * bigint(rhs.p);
        return lv < rv;
    }
};

template <class T> bool chmin(T& a, T b) {
    if (a < b) return false;
    a = b;
    return true;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    int n;
    bigint h;
    std::cin >> n >> h;
    std::vector<std::pair<bigint, bigint>> ps(n);
    std::vector<bigint> xs;
    for (auto& [x, y] : ps) {
        std::cin >> x >> y;
        xs.emplace_back(x);
    }
    std::sort(all(xs));
    xs.erase(std::unique(all(xs)), xs.end());
    std::vector<int> arg_ord;
    {
        std::vector<std::pair<bigint, bigint>> ret;
        std::map<std::pair<bigint, bigint>, int> map;
        rep(i, 0, n) {
            auto [x, y] = ps[i];
            ret.emplace_back(2 * x, 2 * y - h);
            map[{2 * x, 2 * y - h}] = i;
        }
        arg_sort_ll(ret);
        for (auto [x, y] : ret) {
            arg_ord.emplace_back(map[{x, y}]);
        }
    }
    rational ans = rational(100000000000);
    auto calc_score = [](const std::vector<bigint>& a) -> rational {
        if (a[2] == 0) {
            assert(a[1] == 0 && a[2] == 0);
            return bigint(0);
        }
        return rational(a[0]) - rational(a[1] * a[1]) / rational(4 * a[2]);
    };
    for (auto tx : xs) {
        std::vector<bigint> u(3, 0), d(3, 0);
        for (auto [x, y] : ps) {
            if (x <= tx) {
                u[0] += (y - h) * (y - h);
                u[1] += -2 * x * (y - h);
                u[2] += x * x;
            } else {
                d[0] += y * y;
                d[1] += -2 * x * y;
                d[2] += x * x;
            }
        }
        chmin(ans, calc_score(u) + calc_score(d));
        for (auto i : arg_ord) {
            auto [x, y] = ps[i];
            if (x <= tx) {
                u[0] -= (y - h) * (y - h);
                u[1] -= -2 * x * (y - h);
                u[2] -= x * x;

                d[0] += y * y;
                d[1] += -2 * x * y;
                d[2] += x * x;
            } else {
                d[0] -= y * y;
                d[1] -= -2 * x * y;
                d[2] -= x * x;

                u[0] += (y - h) * (y - h);
                u[1] += -2 * x * (y - h);
                u[2] += x * x;
            }
            chmin(ans, calc_score(u) + calc_score(d));
        }
    }
    auto [p, q] = ans.val();
    std::cout << ld(p) / ld(q) << '\n';
}
0