結果

問題 No.1008 Bench Craftsman
ユーザー simansiman
提出日時 2023-06-20 18:20:18
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,129 bytes
コンパイル時間 3,180 ms
コンパイル使用メモリ 105,616 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2023-09-10 14:08:24
合計ジャッジ時間 8,856 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,696 KB
testcase_01 AC 2 ms
5,772 KB
testcase_02 AC 3 ms
5,900 KB
testcase_03 AC 2 ms
5,860 KB
testcase_04 AC 3 ms
5,828 KB
testcase_05 AC 204 ms
9,792 KB
testcase_06 AC 67 ms
7,276 KB
testcase_07 AC 3 ms
5,780 KB
testcase_08 AC 8 ms
5,820 KB
testcase_09 AC 79 ms
6,820 KB
testcase_10 AC 193 ms
9,844 KB
testcase_11 AC 197 ms
9,808 KB
testcase_12 AC 199 ms
9,808 KB
testcase_13 AC 194 ms
9,900 KB
testcase_14 AC 194 ms
9,888 KB
testcase_15 AC 112 ms
9,844 KB
testcase_16 AC 192 ms
9,848 KB
testcase_17 AC 114 ms
9,808 KB
testcase_18 AC 217 ms
9,892 KB
testcase_19 AC 202 ms
9,856 KB
testcase_20 AC 74 ms
7,216 KB
testcase_21 AC 75 ms
7,384 KB
testcase_22 AC 97 ms
7,408 KB
testcase_23 AC 137 ms
8,716 KB
testcase_24 AC 133 ms
8,168 KB
testcase_25 AC 71 ms
7,356 KB
testcase_26 AC 55 ms
6,844 KB
testcase_27 AC 77 ms
7,208 KB
testcase_28 AC 112 ms
8,252 KB
testcase_29 AC 164 ms
9,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MAX_N = 100010;

int N, M;
int A[MAX_N];
vector<int> W[MAX_N];
vector<P> Q;

bool f(int c) {
  int cnt = 0;
  int weight = 0;
  vector<int> sum(N, 0);
  vector<int> counter(N + 1, 0);
  vector<int> remain(N + 1, 0);

  for (int i = 0; i < N; ++i) {
    cnt -= counter[i];
    weight -= remain[i];

    weight -= c * cnt;
    sum[i] += weight;

    for (int w : W[i]) {
      int dist = (c == 0) ? N : w / c;
      if (dist == 0) continue;

      weight += w;
      int n_idx = min(N, i + 1 + dist);
      counter[n_idx] += 1;
      if (c > 0) remain[n_idx] += w % c;
      cnt += 1;
    }
  }

  cnt = 0;
  weight = 0;
  fill(counter.begin(), counter.end(), 0);
  fill(remain.begin(), remain.end(), 0);

  for (int i = N - 1; i >= 0; --i) {
    cnt -= counter[i];
    weight -= remain[i];

    weight -= c * cnt;
    sum[i] += weight;

    for (int w : W[i]) {
      int dist = (c == 0) ? 0 : w / c;
      if (dist == 0) continue;

      int n_idx = max(-1, i - 1 - dist);
      weight += w;
      cnt += 1;

      if (n_idx >= 0) {
        counter[n_idx] += 1;
        if (c > 0) remain[n_idx] += w % c;
      }
    }
  }

  for (P &p : Q) {
    sum[p.first] += p.second;
  }

  for (int i = 0; i < N; ++i) {
    if (sum[i] >= A[i]) return false;
  }

  return true;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  int max_w = 0;
  for (int i = 0; i < M; ++i) {
    int x, w;
    cin >> x >> w;
    --x;
    max_w = max(max_w, w);
    Q.push_back(P(x, w));
    W[x].push_back(w);
  }

  if (f(0)) {
    cout << 0 << endl;
  } else if (not f(max_w)) {
    cout << -1 << endl;
  } else {
    int ok = max_w;
    int ng = 0;

    while (abs(ok - ng) >= 2) {
      int c = (ok + ng) / 2;

      if (f(c)) {
        ok = c;
      } else {
        ng = c;
      }
    }

    cout << ok << endl;
  }

  return 0;
}
0