結果

問題 No.609 Noelちゃんと星々
ユーザー けーむけーむ
提出日時 2020-03-19 12:14:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 2,159 ms
コンパイル使用メモリ 174,040 KB
実行使用メモリ 4,972 KB
最終ジャッジ日時 2023-08-20 20:45:29
合計ジャッジ時間 4,457 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
4,600 KB
testcase_01 AC 15 ms
4,380 KB
testcase_02 AC 11 ms
4,380 KB
testcase_03 AC 14 ms
4,376 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 35 ms
4,972 KB
testcase_06 AC 31 ms
4,680 KB
testcase_07 AC 19 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 22 ms
4,384 KB
testcase_11 AC 21 ms
4,376 KB
testcase_12 AC 26 ms
4,680 KB
testcase_13 AC 6 ms
4,384 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 31 ms
4,736 KB
testcase_16 AC 36 ms
4,860 KB
testcase_17 AC 29 ms
4,640 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 30 ms
4,640 KB
testcase_20 AC 37 ms
4,932 KB
testcase_21 AC 36 ms
4,916 KB
testcase_22 AC 36 ms
4,912 KB
testcase_23 AC 37 ms
4,920 KB
testcase_24 AC 37 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

template<class T>
struct Median {
private:
    priority_queue<T> left;
    priority_queue<T, vector<T>, greater<T>> right;
    T min_value = numeric_limits<T>::min();
    T max_value = numeric_limits<T>::max();
    
public:
    Median() {
        left.push(min_value);
        right.push(max_value);
    }
    
    void push(T value) {
        T values[3];
        values[0] = left.top(); left.pop();
        values[1] = value;
        values[2] = right.top(); right.pop();
        sort(values, values + 3);
        left.push(values[0]);
        right.push(values[2]);
        if (left.size() == right.size()) {
            left.push(values[1]);
        }
        else {
            right.push(values[1]);
        }
    }
    
    pair<T, T> get() {
        pair<T, T> result;
        if (left.size() != right.size()) {
            result.first = result.second = left.top();
        }
        else {
            result.first = left.top();
            result.second = right.top();
        }
        return result;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    Median<ll> m;
    rep(i, n) m.push(a[i]);
    auto me = m.get();
    ll ans = 0;
    rep(i, n) {
        ans += abs(me.first - a[i]);
    }
    cout << ans << endl;
    return 0;
}
0