結果

問題 No.1374 Absolute Game
ユーザー ninoinuininoinui
提出日時 2021-02-05 22:15:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 208,456 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-15 08:30:38
合計ジャッジ時間 3,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 19 ms
4,380 KB
testcase_10 AC 22 ms
4,380 KB
testcase_11 AC 14 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 26 ms
4,380 KB
testcase_15 AC 25 ms
4,376 KB
testcase_16 AC 25 ms
4,376 KB
testcase_17 AC 26 ms
4,380 KB
testcase_18 AC 25 ms
4,376 KB
testcase_19 AC 26 ms
4,376 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 13 ms
4,376 KB
testcase_22 AC 25 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 5 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 11 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  long N;
  cin >> N;
  vector<long> A(N);
  for (long i = 0; i < N; i++) cin >> A.at(i);

  auto f1 = [&]() {
    sort(A.begin(), A.end());
    long x = 0, y = 0;
    for (long i = 0; i < N; i++) {
      if (i % 2) y += A.at(i);
      else x += A.at(i);
    }
    return abs(x) - abs(y);
  };

  auto f2 = [&]() {
    sort(A.begin(), A.end());
    long acc = accumulate(A.begin(), A.end(), 0L);
    long x = accumulate(A.begin(), A.begin() + (N + 1) / 2, 0L);
    long y = acc - x;
    return abs(x) - abs(y);
  };

  auto f3 = [&]() {
    sort(A.rbegin(), A.rend());
    long x = 0, y = 0;
    for (long i = 0; i < N; i++) {
      if (i % 2) y += A.at(i);
      else x += A.at(i);
    }
    return abs(x) - abs(y);
  };

  auto f4 = [&]() {
    sort(A.rbegin(), A.rend());
    long acc = accumulate(A.begin(), A.end(), 0L);
    long x = accumulate(A.begin(), A.begin() + (N + 1) / 2, 0L);
    long y = acc - x;
    return abs(x) - abs(y);
  };

  long a = f1();
  long b = f2();
  long c = f3();
  long d = f4();

  if (min(a, b) > min(c, d)) cout << min(a, b) << '\n';
  else cout << min(c, d) << '\n';
}
0