結果

問題 No.837 Noelちゃんと星々2
ユーザー YamaKasaYamaKasa
提出日時 2019-06-15 02:48:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 938 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 171,572 KB
実行使用メモリ 4,740 KB
最終ジャッジ日時 2023-09-02 07:23:53
合計ジャッジ時間 3,193 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
4,676 KB
testcase_01 AC 22 ms
4,688 KB
testcase_02 AC 23 ms
4,624 KB
testcase_03 AC 23 ms
4,572 KB
testcase_04 AC 23 ms
4,672 KB
testcase_05 AC 23 ms
4,740 KB
testcase_06 AC 23 ms
4,700 KB
testcase_07 AC 23 ms
4,688 KB
testcase_08 AC 23 ms
4,580 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> Y(N);
  for (int i = 0; i < N; i++) {
    cin >> Y[i];
  }
  if (N == 2) {
    if (Y[0] == Y[1]) {
      cout << 1 << "\n";
    } else {
      cout << 0 << "\n";
    }
    return 0;
  }
  sort(Y.begin(), Y.end());
  if (Y[0] == Y[N - 1]) {
    cout << "1\n";
    return 0;
  }
  if (Y[0] == Y[N - 2]) {
    cout << "0\n";
    return 0;
  }
  ll ans = pow(10, 15);
  ll S[N + 1]{};
  for (int i = 0; i < N; i++) {
    S[i + 1] += S[i] + Y[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int m1 = i / 2;
    int m2 = (N + i) / 2;
    ll cost = (m1 + 1) * Y[m1] - S[m1 + 1] + S[i + 1] - S[m1 + 1] - (i - m1) * Y[m1];
    cost += (m2 - i) * Y[m2] - (S[m2 + 1] - S[i + 1]) + (S[N] - S[m2 + 1]) - (N - 1- m2) * Y[m2];
    ans = min(ans, cost);
  }
  cout << ans << "\n";
  return 0;
}
0