結果

問題 No.2438 Double Least Square
ユーザー ebi_flyebi_fly
提出日時 2023-08-13 15:22:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 3,389 bytes
コンパイル時間 2,450 ms
コンパイル使用メモリ 228,280 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-05 05:08:39
合計ジャッジ時間 3,548 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 15 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 15 ms
5,376 KB
testcase_13 AC 15 ms
5,376 KB
testcase_14 AC 16 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 15 ms
5,376 KB
testcase_21 AC 14 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 16 ms
5,376 KB
testcase_24 AC 16 ms
5,376 KB
testcase_25 AC 15 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 16 ms
5,376 KB
testcase_28 AC 16 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

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

using i64 = std::int64_t;

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;
}

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;
    i64 h;
    std::cin >> n >> h;
    std::vector<std::pair<i64,i64>> ps(n);
    std::vector<i64> 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<i64,i64>> ret;
        std::map<std::pair<i64, i64>, 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}]);
        }
    }
    using ld = long double;
    ld ans = std::numeric_limits<ld>::max();
    auto calc_score = [](const std::vector<i64> &a) -> ld {
        if(a[2] == 0) {
            assert(a[1] == 0 && a[2] == 0);
            return 0;
        }
        return ld(a[0]) - ld(a[1] * a[1]) / ld(4 * a[2]);
    };
    for(auto tx: xs) {
        std::vector<i64> 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));
        }
    }
    std::cout << ans << '\n';
}
0