結果

問題 No.275 中央値を求めよ
ユーザー SlephySlephy
提出日時 2024-01-09 08:26:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,258 bytes
コンパイル時間 3,707 ms
コンパイル使用メモリ 209,364 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-09 08:26:53
合計ジャッジ時間 4,758 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = (int)1e9 + 1001010;
constexpr ll llINF = (ll)4e18 + 22000020;
const string endn = "\n";
template <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}
const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b){if(b < 0) a = -a, b = -b; return a >= 0 ? a/b : (a+1)/b - 1;}
ll ceil (ll a, ll b){if(b < 0) a = -a, b = -b; return a > 0 ? (a-1)/b + 1 : a/b;}
ll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}
// ================================== ここまでテンプレ ==================================

// 昇順または降順にソート済みの配列を入力とする
// floor(中央値)を返す
template<class T>
T median(const vector<T> &v){
    // Tが整数型であることを確認
    static_assert(is_integral<T>::value, "T must be integral");
    int n = v.size();
    const T &l = v[n/2 - 1];
    const T &r = v[n/2];
    if(n%2 == 1) return r;
    else if(l > 0 != r > 0) return floor(l+r, 2);
    else return l + floor(r-l, 2);
}

// 昇順または降順にソート済みの配列を入力とする
template<class T>
string median_string(const vector<T> &v){
    // Tが整数型であることを確認
    static_assert(is_integral<T>::value, "T must be integral");
    int n = v.size();
    const T &l = v[n/2 - 1];
    const T &r = v[n/2];
    T mid;
    bool is_odd = false;
    if(n%2 == 1) mid = r;
    else if(l > 0 != r > 0){
        mid = floor(l+r, 2);
        is_odd = (l + r) % 2;
    }
    else{
        mid = l + floor(r-l, 2);
        is_odd = (r - l) % 2;
    }

    string ret;
    if(is_odd){
        if(mid < 0) mid++;
        ret = to_string(mid) + (string)".5";
    }
    else{
        ret = to_string(mid);
    }
    return ret;
}

// 昇順または降順にソート済みの配列を入力とする
template<class T>
double median_double(const vector<T> &v){
    int n = v.size();
    double l = v[n/2 - 1];
    double r = v[n/2];
    if(n%2 == 1) return r;
    else if(l > 0 != r > 0) return (l + r) / 2;
    else return l - (l - r) / 2;
}

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n; cin >> n;
    vector<ll> a(n); cin >> a;
    sort(a.begin(), a.end());
    string ans = median_string(a);
    cout << ans << endn;
    return 0;
}
0