結果

問題 No.972 選び方のスコア
ユーザー kimiyukikimiyuki
提出日時 2020-01-18 02:54:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 1,225 bytes
コンパイル時間 2,200 ms
コンパイル使用メモリ 204,256 KB
実行使用メモリ 8,056 KB
最終ジャッジ日時 2023-09-08 13:23:25
合計ジャッジ時間 7,107 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
7,700 KB
testcase_01 AC 117 ms
7,764 KB
testcase_02 AC 110 ms
7,660 KB
testcase_03 AC 54 ms
5,464 KB
testcase_04 AC 110 ms
7,792 KB
testcase_05 AC 109 ms
7,812 KB
testcase_06 AC 109 ms
7,748 KB
testcase_07 AC 88 ms
6,536 KB
testcase_08 AC 97 ms
7,748 KB
testcase_09 AC 95 ms
7,472 KB
testcase_10 AC 100 ms
7,748 KB
testcase_11 AC 104 ms
8,012 KB
testcase_12 AC 105 ms
7,784 KB
testcase_13 AC 105 ms
7,920 KB
testcase_14 AC 105 ms
7,800 KB
testcase_15 AC 105 ms
8,052 KB
testcase_16 AC 106 ms
7,780 KB
testcase_17 AC 105 ms
8,052 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 105 ms
8,056 KB
testcase_20 AC 104 ms
7,848 KB
testcase_21 AC 105 ms
7,844 KB
testcase_22 AC 104 ms
7,512 KB
testcase_23 AC 104 ms
7,884 KB
testcase_24 AC 105 ms
7,828 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
template <typename T> istream & operator >> (istream & in, vector<T> & xs) { REP (i, xs.size()) { in >> xs[i]; } return in; }

template <typename UnaryPredicate>
int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) {
    assert (l <= r);
    -- l;
    while (r - l > 1) {
        int64_t m = l + (r - l) / 2;
        (p(m) ? r : l) = m;
    }
    return r;
}

int64_t solve(int n, vector<int64_t> a) {
    sort(ALL(a));
    vector<int64_t> acc(n + 1);
    partial_sum(ALL(a), acc.begin() + 1);

    int64_t ans = INT64_MIN;
    REP (i, n) {
        int k_max = min(i, n - i - 1);
        auto f = [&](int k) {
            return a[i] + (acc[i] - acc[i - k]) + (acc[n] - acc[n - k]) - (2 * k + 1) * a[i];
        };
        int k = binsearch(0, k_max, [&](int k) {
            return f(k + 1) - f(k) < 0;
        });
        chmax(ans, f(k));
    }
    return ans;
}

int main() {
    int n; cin >> n;
    vector<int64_t> a(n); cin >> a;
    cout << solve(n, a) << endl;
    return 0;
}
0