結果

問題 No.1120 Strange Teacher
ユーザー 0w1
提出日時 2020-12-19 14:06:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 1,000 ms
コード長 1,088 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 198,692 KB
最終ジャッジ日時 2025-01-17 03:50:24
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  ios::sync_with_stdio(false);

  int N; { cin >> N; } 

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

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

  if (N == 2) {
    if (A[0] - B[0] == B[1] - A[0]) {
      cout << abs(A[0] - B[0]) << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int64_t dSum = 0; {
    for (int i = 0; i < N; ++i) dSum += A[i] - B[i];
  }

  if (dSum < 0 || dSum % (N - 2)) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> dord(N); {
    iota(dord.begin(), dord.end(), 0);
    sort(dord.begin(), dord.end(), [&](int i, int j) { return B[i] - A[i] > B[j] - A[j]; });
  }

  int64_t dGlobal = 0;
  int64_t opCnt = dSum / (N - 2);
  for (int i : dord) {
    int64_t d = B[i] - (A[i] - dGlobal);
    int64_t q = (opCnt + d) / 2;

    if (opCnt + d % 2 == 1 || q < 0 || opCnt < q) {
      cout << -1 << endl;
      return 0;
    }
    
    opCnt -= q;
    dGlobal += q;
  }

  assert(opCnt == 0);
  cout << (dSum / (N - 2)) << endl;
}

0