結果

問題 No.837 Noelちゃんと星々2
ユーザー ktr216ktr216
提出日時 2019-06-14 22:16:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 855 bytes
コンパイル時間 1,730 ms
コンパイル使用メモリ 166,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 17:04:51
合計ジャッジ時間 3,437 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
5,376 KB
testcase_25 WA -
testcase_26 AC 2 ms
5,376 KB
testcase_27 WA -
testcase_28 AC 2 ms
5,376 KB
testcase_29 WA -
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"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

ll Y[100000], N;

ll dif(int x) {
    ll sum1 = 0;
    rep(i, x) sum1 += Y[i];
    ll mean1 = sum1 / x;
    if ((sum1 - mean1 * x) * 2 >= x) mean1++;
    ll sum2 = 0;
    rep(i, N - x) sum2 += Y[i + x];
    ll mean2 = sum2 / (N - x);
    if ((sum2 - mean2 * (N - x)) * 2 >= N - x) mean2++;
    ll ret = 0;
    rep(i, x) ret += abs(Y[i] - mean1);
    rep(i, N - x) ret += abs(Y[i + x] - mean2);
    return ret;
}

ll solve() {
    if (Y[0] == Y[N - 1]) return 1;
    int l = 1, r = N - 1;
    while (l < r - 1) {
        int m = (l + r) / 2;
        if (dif(m - 1) <= dif(m)) r = m;
        else l = m;
    }
    return min(dif(l), dif(r));
}

int main() {
    cin >> N;
    rep(i, N) cin >> Y[i];
    sort(Y, Y + N);
    cout << solve() << endl;
}
0