結果

問題 No.837 Noelちゃんと星々2
ユーザー kyunakyuna
提出日時 2019-07-16 19:46:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 789 bytes
コンパイル時間 640 ms
コンパイル使用メモリ 76,056 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-11 14:25:44
合計ジャッジ時間 2,160 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
    int n; cin >> n;
    vector<ll> a(n);
    for (ll &ai: a) cin >> ai;
    sort(a.begin(), a.end());
    if (a[0] == a[n - 1]) {
        cout << 1 << endl;
        return 0;
    }
    vector<ll> acc(n + 1);
    for (int i = 0; i < n; i++) acc[i + 1] = acc[i] + a[i];
    ll ans = 1LL << 60;
    for (int i = 0; i < n; i++) {
        int m1 = i / 2;
        int m2 = (n + i) / 2;
        ll s1 = a[m1] * m1 - acc[m1];
        ll s2 = (acc[i] - acc[m1]) - a[m1] * (i - m1);
        ll s3 = a[m2] * (m2 - i) - (acc[m2] - acc[i]);
        ll s4 = (acc[n] - acc[m2]) - a[m2] * (n - m2);
        ans = min(ans, s1 + s2 + s3 + s4);
    }
    cout << ans << endl;
    return 0;
}
0