結果

問題 No.1008 Bench Craftsman
ユーザー 👑 emthrmemthrm
提出日時 2020-03-06 22:25:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 776 ms / 2,000 ms
コード長 3,313 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 206,352 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-22 08:49:57
合計ジャッジ時間 14,529 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 776 ms
11,392 KB
testcase_06 AC 249 ms
10,764 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 100 ms
5,376 KB
testcase_10 AC 745 ms
11,520 KB
testcase_11 AC 741 ms
11,520 KB
testcase_12 AC 746 ms
11,392 KB
testcase_13 AC 701 ms
11,392 KB
testcase_14 AC 747 ms
11,520 KB
testcase_15 AC 33 ms
5,376 KB
testcase_16 AC 733 ms
11,520 KB
testcase_17 AC 33 ms
5,376 KB
testcase_18 AC 657 ms
11,392 KB
testcase_19 AC 712 ms
11,520 KB
testcase_20 AC 226 ms
7,808 KB
testcase_21 AC 241 ms
6,928 KB
testcase_22 AC 341 ms
5,892 KB
testcase_23 AC 478 ms
8,540 KB
testcase_24 AC 479 ms
7,424 KB
testcase_25 AC 232 ms
9,508 KB
testcase_26 AC 170 ms
8,096 KB
testcase_27 AC 277 ms
5,376 KB
testcase_28 AC 381 ms
8,852 KB
testcase_29 AC 587 ms
9,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Abelian>
struct BITRangeAdd {
  BITRangeAdd(int n_, const Abelian UNITY = 0) : n(n_), UNITY(UNITY) {
    ++n;
    dat_const.assign(n, UNITY);
    dat_linear.assign(n, UNITY);
  }

  void add(int left, int right, Abelian val) {
    if (right < ++left) return;
    for (int i = left; i < n; i += i & -i) {
      dat_const[i] -= val * (left - 1);
      dat_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      dat_const[i] += val * right;
      dat_linear[i] -= val;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i];
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) res += dat_const[i];
    return res;
  }

  Abelian sum(int left, int right) {
    if (right <= left) return UNITY;
    return sum(right) - sum(left);
  }

  Abelian operator[](const int idx) { return sum(idx, idx + 1); }

private:
  int n;
  const Abelian UNITY;
  vector<Abelian> dat_const, dat_linear;
};

int main() {
  int n, m; cin >> n >> m;
  vector<int> a(n); REP(i, n) cin >> a[i];
  vector<int> x(m), w(m); REP(i, m) cin >> x[i] >> w[i], --x[i];
  vector<ll> bench(n, 0);
  REP(i, m) bench[x[i]] += w[i];
  REP(i, n) {
    if (bench[i] >= a[i]) {
      cout << "-1\n";
      return 0;
    }
  }
  if (*min_element(ALL(a)) > accumulate(ALL(w), 0LL)) {
    cout << "0\n";
    return 0;
  }
  ll lb = 0, ub = 100000;
  while (ub - lb > 1) {
    ll mid = (lb + ub) >> 1;
    BITRangeAdd<ll> ci(n), left(n), right(n);
    REP(i, m) {
      int ran = w[i] / mid;
      int l = min(x[i], ran), r = min(n - 1 - x[i], ran);
      ci.add(x[i] - l, x[i] + r + 1, w[i]);
      left.add(x[i] - l, x[i], -1);
      if (x[i] - l > 0) left.add(x[i] - l - 1, x[i] - l, l);
      right.add(x[i] + 1, x[i] + r + 1, -1);
      if (x[i] + r + 1 < n) right.add(x[i] + r + 1, x[i] + r + 2, r);
    }
    vector<ll> L(n), R(n);
    REP(i, n) L[i] = left[i];
    for (int i = n - 2; i >= 0; --i) L[i] += L[i + 1];
    REP(i, n) R[i] = right[i];
    FOR(i, 1, n) R[i] += R[i - 1];
    bool ok = true;
    // cout << mid << ":\n";
    // REP(i, n) cout << ci[i] << " \n"[i + 1 == n];
    // REP(i, n) cout << L[i] << " \n"[i + 1 == n];
    // REP(i, n) cout << R[i] << " \n"[i + 1 == n];
    REP(i, n) {
      ll load = ci[i] + (L[i] + R[i]) * mid;
      // cout << load << " \n"[i + 1 == n];
      ok &= load < a[i];
    }
    (ok ? ub : lb) = mid;
  }
  cout << ub << '\n';
  return 0;
}
0