結果

問題 No.1374 Absolute Game
ユーザー RheoTommyRheoTommy
提出日時 2021-02-05 23:12:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,798 bytes
コンパイル時間 2,215 ms
コンパイル使用メモリ 210,264 KB
実行使用メモリ 5,520 KB
最終ジャッジ日時 2023-09-15 10:44:45
合計ジャッジ時間 4,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 16 ms
4,380 KB
testcase_09 AC 40 ms
5,000 KB
testcase_10 AC 45 ms
5,260 KB
testcase_11 AC 28 ms
4,380 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 35 ms
4,512 KB
testcase_14 AC 52 ms
5,496 KB
testcase_15 AC 53 ms
5,424 KB
testcase_16 AC 52 ms
5,452 KB
testcase_17 AC 52 ms
5,380 KB
testcase_18 AC 52 ms
5,520 KB
testcase_19 AC 52 ms
5,308 KB
testcase_20 AC 20 ms
4,376 KB
testcase_21 AC 27 ms
4,404 KB
testcase_22 AC 51 ms
5,520 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 10 ms
4,380 KB
testcase_26 AC 16 ms
4,376 KB
testcase_27 AC 23 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int) n; i++)

using ll = long long;
using namespace std;

const long long INF = 1ll << 60;

void chmax(ll &x, ll y) {
    x = max(x, y);
}

void chmin(ll &x, ll y) {
    x = min(x, y);
}

int main() {
    ll n;
    cin >> n;
    vector<ll> d;
    rep(i, n) {
        ll ci;
        cin >> ci;
        d.push_back(ci);
    };
    
    sort(d.begin(), d.end());
    
    deque<ll> c;
    for (auto di:d) c.push_back(di);
    
    ll ans = INF;
    // big big
    {
        deque<ll> ci = c;
        vector<ll> score(2, 0);
        rep(i, n) {
            score[i % 2] += ci.back();
            ci.pop_back();
        }
        chmin(ans, abs(score[0]) - abs(score[1]));
    }
    // big small
    {
        deque<ll> ci = c;
        vector<ll> score(2, 0);
        rep(i, n) {
            if (i % 2) {
                score[i % 2] += ci.front();
                ci.pop_front();
            } else {
                score[i % 2] += ci.back();
                ci.pop_back();
            }
        }
        chmin(ans, abs(score[0]) - abs(score[1]));
    }
    
    ll ans2 = -INF;
    // small big
    {
        deque<ll> ci = c;
        vector<ll> score(2, 0);
        rep(i, n) {
            if (i % 2) {
                score[i % 2] += ci.back();
                ci.pop_back();
            } else {
                score[i % 2] += ci.front();
                ci.pop_front();
            }
        }
        chmax(ans2, abs(score[0]) - abs(score[1]));
    }
    //  small small
    {
        deque<ll> ci = c;
        vector<ll> score(2, 0);
        rep(i, n) {
            score[i % 2] += ci.front();
            ci.pop_front();
        }
        chmin(ans2, abs(score[0]) - abs(score[1]));
    }
    cout << max(ans, ans2) << endl;
}
0