結果

問題 No.972 選び方のスコア
ユーザー shibh308shibh308
提出日時 2019-12-06 15:12:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,863 bytes
コンパイル時間 3,561 ms
コンパイル使用メモリ 209,768 KB
実行使用メモリ 34,228 KB
最終ジャッジ日時 2023-08-22 19:34:13
合計ジャッジ時間 7,507 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
34,224 KB
testcase_01 AC 143 ms
34,224 KB
testcase_02 AC 136 ms
32,968 KB
testcase_03 AC 67 ms
17,816 KB
testcase_04 AC 134 ms
34,144 KB
testcase_05 AC 134 ms
34,088 KB
testcase_06 AC 136 ms
34,128 KB
testcase_07 AC 107 ms
26,440 KB
testcase_08 AC 123 ms
34,132 KB
testcase_09 AC 120 ms
33,072 KB
testcase_10 AC 126 ms
34,180 KB
testcase_11 AC 133 ms
34,172 KB
testcase_12 AC 132 ms
34,168 KB
testcase_13 AC 131 ms
34,128 KB
testcase_14 AC 130 ms
34,132 KB
testcase_15 AC 131 ms
34,140 KB
testcase_16 AC 132 ms
34,168 KB
testcase_17 AC 133 ms
34,176 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 131 ms
34,228 KB
testcase_20 AC 130 ms
34,148 KB
testcase_21 AC 130 ms
34,092 KB
testcase_22 AC 129 ms
33,200 KB
testcase_23 AC 132 ms
34,180 KB
testcase_24 AC 130 ms
34,192 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

using i64 = long long;

template <typename T>
struct DisjointSparseTable{
    function<T(T, T)> f;
    vector<vector<T>> v;

    DisjointSparseTable(vector<T>& inp, function<T(T, T)> f) : f(f){
        int n = inp.size();
        int b;
        for(b = 0; (1 << b) <= inp.size(); ++b);
        v.assign(b, vector<T>(n));
        for(int i = 0; i < n; ++i)
            v[0][i] = inp[i];
        for(int i = 1; i < b; ++i){
            int siz = 1 << i;
            for(int j = 0; j < n; j += siz << 1){
                int t = min(j + siz, n);
                v[i][t - 1] = inp[t - 1];
                for(int k = t - 2; k >= j; --k)
                    v[i][k] = f(inp[k], v[i][k + 1]);
                if(t >= n)
                    break;
                v[i][t] = inp[t];
                int r = min(t + siz, n);
                for(int k = t + 1; k < r; ++k)
                    v[i][k] = f(v[i][k - 1], inp[k]);
            }
        }
    }

    T get(int l, int r){
        if(l >= --r)
            return v[0][l];
        int p = 31 - __builtin_clz(l ^ r);
        return f(v[p][l], v[p][r]);
    }
};


signed main(){
    int n;
    cin >> n;
    vector<i64> a(n);
    for(auto& x : a)
        cin >> x;
    sort(a.begin(), a.end());
    DisjointSparseTable<i64> d(a, [](auto x, auto y){return x + y;});

    auto f = [&](int i, int len){
        int x = a[i - len];
        int y = a[n - len];
        return x + y - 2 * a[i] >= 0;
    };

    i64 ans = 0;
    for(int i = 1; i < n - 1; ++i){
        int ok = 0, ng = min(i + 1, n - i);
        while(ng - ok > 1){
            int mid = (ok + ng) / 2;
            (f(i, mid) ? ok : ng) = mid;
        }
        if(ok == 0)
            continue;
        ans = max(ans, d.get(n - ok, n) + d.get(i - ok, i) - a[i] * 2 * ok);
    }
    cout << ans << endl;
}
0