結果

問題 No.1008 Bench Craftsman
ユーザー MayimgMayimg
提出日時 2020-03-07 02:57:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 2,847 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 195,976 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-22 11:45:58
合計ジャッジ時間 6,317 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
11,520 KB
testcase_01 AC 3 ms
7,424 KB
testcase_02 AC 21 ms
11,520 KB
testcase_03 AC 27 ms
11,520 KB
testcase_04 AC 24 ms
11,520 KB
testcase_05 AC 137 ms
12,896 KB
testcase_06 AC 47 ms
11,648 KB
testcase_07 AC 20 ms
11,648 KB
testcase_08 AC 5 ms
7,680 KB
testcase_09 AC 109 ms
12,896 KB
testcase_10 AC 140 ms
13,020 KB
testcase_11 AC 142 ms
13,056 KB
testcase_12 AC 141 ms
12,892 KB
testcase_13 AC 137 ms
12,892 KB
testcase_14 AC 146 ms
13,020 KB
testcase_15 AC 104 ms
13,016 KB
testcase_16 AC 130 ms
13,020 KB
testcase_17 AC 107 ms
12,800 KB
testcase_18 AC 153 ms
12,928 KB
testcase_19 AC 145 ms
13,020 KB
testcase_20 AC 61 ms
12,032 KB
testcase_21 AC 65 ms
11,904 KB
testcase_22 AC 104 ms
12,672 KB
testcase_23 AC 109 ms
12,800 KB
testcase_24 AC 117 ms
12,928 KB
testcase_25 AC 49 ms
11,520 KB
testcase_26 AC 43 ms
11,520 KB
testcase_27 AC 88 ms
12,544 KB
testcase_28 AC 83 ms
12,160 KB
testcase_29 AC 118 ms
12,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1LL << 60;
constexpr int geta = 2e5;
constexpr int N = 505050, M = 101010;
long long a[N], x[M], w[M];
long long imos[N];
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  fill(a, a + N, INF);
  for (int i = 1; i <= n; i++) cin >> a[i + geta];
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> w[i];
    x[i] += geta;
    sum += w[i];
  }
  bool zero = true;
  for (int i = 0; i < N; i++) {
    if (a[i] <= sum) zero = false;
  }
  if (zero) {
    cout << 0 << endl;
    return 0;
  }
  auto judge = [&] (long long d) {
    for (int i = 0; i < N; i++) imos[i] = 0;
    for (int i = 0; i < m; i++) {
      long long pos_fi = x[i] - w[i] / d;
      long long pos_la = x[i];
      if (pos_fi < pos_la) {
        imos[pos_fi + 1] += d;
      }
      if (pos_fi < pos_la) {
        imos[pos_la + 1] -= d;
      }
    }
    for (int i = 0; i < m; i++) {
      long long fi = max(0LL, w[i] - d);
      long long pos_fi = x[i] + 1;
      long long pos_la = x[i] + 1 + fi / d;
      if (pos_fi < pos_la) {
        imos[pos_fi + 1] += -d;
      }
      if (pos_fi < pos_la) {
        imos[pos_la + 1] -= -d;
      }
    }
    // cerr << d << endl;
    // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " ";
    // cerr << endl;
    for (int i = 1; i < N; i++) imos[i] += imos[i - 1];
    // cerr << d << endl;
    // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " ";
    // cerr << endl;
    for (int i = 0; i < m; i++) {
      long long fi = w[i] % d;
      long long pos_fi = x[i] - w[i] / d;
      long long pos_la = x[i];
      imos[pos_fi] += fi;
      imos[pos_la + 1] -= fi;
      if (pos_fi < pos_la) {
        imos[pos_la + 1] -= d * (pos_la - pos_fi);
      }
    }
    // cerr << d << endl;
    // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " ";
    // cerr << endl;
    for (int i = 0; i < m; i++) {
      long long fi = max(0LL, w[i] - d);
      long long pos_fi = x[i] + 1;
      long long pos_la = x[i] + 1 + fi / d;
      imos[pos_fi] += fi;
      imos[pos_la + 1] -= fi;
      if (pos_fi < pos_la) {
        imos[pos_la + 1] -= -d * (pos_la - pos_fi);
      }
    }
    // cerr << d << endl;
    // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " ";
    // cerr << endl;
    for (int i = 1; i < N; i++) imos[i] += imos[i - 1];
    // cerr << d << endl;
    // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " ";
    // cerr << endl;
    for (int i = 0; i < N; i++) {
      if (a[i] <= imos[i]) return false;
    }
    return true;
  };
  long long lo = 0, hi = 2e5;
  while (hi - lo > 1) {
    long long mid = (lo + hi) >> 1;
    if (judge(mid)) hi = mid;
    else lo = mid;
  }
  cout << (hi == 2e5 ? -1 : hi) << endl;
  return 0;
}
0