結果

問題 No.3073 Fraction Median
ユーザー SnowBeenDiding
提出日時 2025-03-22 16:10:06
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,497 bytes
コンパイル時間 6,155 ms
コンパイル使用メモリ 333,812 KB
実行使用メモリ 16,336 KB
最終ジャッジ日時 2025-03-22 16:10:19
合計ジャッジ時間 12,272 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 TLE * 1 -- * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

struct fraction {
    ll p, q;
    fraction(ll P = 0, ll Q = 1) : p(P), q(Q) {
        // ll g = gcd(p, q);
        // p /= g;
        // q /= g;
    }
    bool operator<(const fraction &other) const {
        return p * other.q < other.p * q;
    }
    bool operator<=(const fraction &other) const {
        return p * other.q <= other.p * q;
    }
    bool operator>(const fraction &other) const {
        return p * other.q > other.p * q;
    }
    bool operator>=(const fraction &other) const {
        return p * other.q >= other.p * q;
    }
    bool operator==(const fraction &other) const {
        return p * other.q == other.p * q;
    }

    fraction operator+(const fraction &other) const {
        return fraction(p * other.q + other.p * q, q * other.q);
    }
    fraction operator-(const fraction &other) const {
        return fraction(p * other.q - other.p * q, q * other.q);
    }
    fraction operator*(const fraction &other) const {
        return fraction(p * other.p, q * other.q);
    }
    fraction operator/(const fraction &other) const {
        return fraction(p * other.q, q * other.p);
    }
    fraction operator+=(const fraction &other) { return *this = *this + other; }
    fraction operator-=(const fraction &other) { return *this = *this - other; }
    fraction operator*=(const fraction &other) { return *this = *this * other; }
    fraction operator/=(const fraction &other) { return *this = *this / other; }
};

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    sort(a.begin(), a.end());
    fraction ans = fraction(1, 1);
    ll tar = (ll)n * (n - 1) / 2;

    auto f = [&](fraction p) {
        ll cnt = 0;
        rep(i, 0, n) {
            int ac = -1, wa = n;
            while (wa - ac > 1) {
                int m = ac + (wa - ac) / 2;
                if (fraction(a[m], a[i]) > p)
                    wa = m;
                else
                    ac = m;
            }
            cnt += ac + 1;
        }
        if (cnt >= tar)
            ans = min(ans, p);
    };

    rep(i, 0, n - 1) {
        fraction p = fraction(a[i + 1], a[i]);
        f(p);
        fraction q = fraction(a[i], a[i + 1]);
        f(q);
    }
    cout << ans.p << " " << ans.q << endl;
}
0