結果

問題 No.1374 Absolute Game
ユーザー vjudge1
提出日時 2024-10-19 02:01:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,605 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 195,376 KB
最終ジャッジ日時 2025-02-24 21:24:42
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:12:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |   scanf("%lld", &n);
      |   ~~~~~^~~~~~~~~~~~
main.cpp:14:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |     scanf("%lld", a + i);
      |     ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define N 100005
typedef long long ll;
typedef unsigned long long ull;
ll n, a[N], s[2], ans = LONG_LONG_MIN, s1, s2;
int main() {
#ifndef ONLINE_JUDGE
  freopen("admin.in", "r", stdin);
  // freopen("admin.out", "w", stdout);
#endif
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i);
  sort(a + 1, a + 1 + n);
  if (n == 1)
    return printf("%lld", abs(a[1])), 0;
  int l, r, now;
  s1 = s2 = LONG_LONG_MAX;
  if (a[1] < 0) {
    if (a[2] < 0) {
      s[0] = s[1] = 0;
      now = 0;
      for (int i = 1; i <= n; i++, now ^= 1)
        s[now] += a[i];
      s1 = abs(s[0]) - abs(s[1]);
    }
    if (a[n] >= 0) {
      s[0] = s[1] = 0;
      l = 1, r = n, now = 0;
      while (l <= r) {
        if (!(now & 1))
          s[0] += a[l++];
        else
          s[1] += a[r--];
        now ^= 1;
      }
      s2 = abs(s[0]) - abs(s[1]);
    }
  }
  if (s1 == s2 && s1 == LONG_LONG_MAX)
    s1 = s2 = 0;
  ans = max(ans, min(s1, s2));
  s1 = s2 = LONG_LONG_MAX;
  if (a[n] >= 0) {
    if (a[1] < 0) {
      s[0] = s[1] = 0;
      l = 1, r = n, now = 0;
      while (l <= r) {
        if (!(now & 1))
          s[0] += a[r--];
        else
          s[1] += a[l++];
        now ^= 1;
      }
      s1 = abs(s[0]) - abs(s[1]);
    }
    if (a[n - 1] >= 0) {
      s[0] = s[1] = 0;
      now = 0;
      for (int i = n; i >= 1; i--, now ^= 1)
        s[now] += a[i];
      s2 = abs(s[0]) - abs(s[1]);
    }
  }
  if (s1 == s2 && s1 == LONG_LONG_MAX)
    s1 = s2 = 0;
  ans = max(ans, min(s1, s2));
  printf("%lld", ans);
  return 0;
}
0