結果

問題 No.2438 Double Least Square
ユーザー noya2noya2
提出日時 2023-08-15 15:22:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,909 bytes
コンパイル時間 2,540 ms
コンパイル使用メモリ 209,792 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-05 05:09:12
合計ジャッジ時間 4,544 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

int main(){
    ll n, h; cin >> n >> h;
    vector<pair<ll,ll>> xys(n);
    for (int i = 0; i < n; i++) cin >> xys[i].first >> xys[i].second;
    sort(xys.begin(),xys.end());
    ld ans = 1e18;
    for (int i = 0; i < n; i++){
        vector<int> uls(n,0); // 0 : lower, 1 : upper
        auto [xi, yi] = xys[i];
        for (int j = 0; j < n; j++){
            auto [xj, yj] = xys[j];
            if ((2*yi-h)*xj + h*xi <= 2*xi*yj) uls[j] = 1;
        }
        array<ll,3> lv = {0,0,0}, uv = {0,0,0};
        auto uadd = [&](int j, ll add_del){
            auto [xj, yj] = xys[j];
            uv[0] += add_del * (h-yj)*(h-yj);
            uv[1] += add_del * 2*xj*(h-yj);
            uv[2] += add_del * xj*xj;
        };
        auto ladd = [&](int j, ll add_del){
            auto [xj, yj] = xys[j];
            lv[0] += add_del * (-yj)*(-yj);
            lv[1] += add_del * 2*xj*(-yj);
            lv[2] += add_del * xj*xj;
        };
        for (int j = 0; j < n; j++){
            if (uls[j] == 0){
                ladd(j,+1);
            }
            else {
                uadd(j,+1);
            }
        }
        auto eval = [](array<ll,3> ar){
            auto [a0, a1, a2] = ar;
            if (a2 == 0) return ld(0);
            ld ans = a0;
            ans -= ld(a1)*a1/(4*a2);
            return ans;
        };
        chmin(ans,eval(lv)+eval(uv));
        for (int j = n-1; j >= 0; j--){
            if (uls[j] == 0){
                ladd(j,-1);
                uadd(j,+1);
            }
            else {
                uadd(j,-1);
                ladd(j,+1);
            }
            chmin(ans,eval(lv)+eval(uv));
        }
    }
    cout << fixed << setprecision(15) << ans << endl;
}
0