結果

問題 No.2835 Take and Flip
ユーザー InTheBloomInTheBloom
提出日時 2024-08-09 21:52:32
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 711 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 105,208 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-08-09 21:52:36
合計ジャッジ時間 3,490 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 88 ms
5,376 KB
testcase_04 AC 93 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 85 ms
5,376 KB
testcase_07 AC 90 ms
5,376 KB
testcase_08 AC 80 ms
5,376 KB
testcase_09 AC 96 ms
5,376 KB
testcase_10 AC 97 ms
5,376 KB
testcase_11 AC 101 ms
5,376 KB
testcase_12 AC 94 ms
5,376 KB
testcase_13 AC 96 ms
5,376 KB
testcase_14 AC 38 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 61 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 75 ms
5,376 KB
testcase_19 AC 90 ms
5,376 KB
testcase_20 AC 34 ms
5,376 KB
testcase_21 AC 86 ms
5,376 KB
testcase_22 AC 63 ms
5,376 KB
testcase_23 AC 30 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

int main () {
    int N; cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    // First君は相手に関係なく残っている最大を選ぶのが最適で、Second君は相手に関係なく残っている最小を選ぶのが最適になる。
    sort(A.begin(), A.end());

    int l = 0, r = N - 1;
    auto is_game_end = [&] () {
        return r < l;
    };

    ll X = 0, Y = 0;
    while (true) {
        X += A[l];
        l++;
        if (is_game_end()) break;

        Y += -A[r];
        r--;
        if (is_game_end()) break;
    }

    cout << X - Y << "\n";
}
0