結果

問題 No.1008 Bench Craftsman
ユーザー tnakao0123tnakao0123
提出日時 2020-03-07 15:19:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,316 ms / 2,000 ms
コード長 3,663 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 99,412 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-04-22 16:22:50
合計ジャッジ時間 18,250 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 1,316 ms
10,112 KB
testcase_06 AC 79 ms
9,344 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 129 ms
5,376 KB
testcase_10 AC 868 ms
9,984 KB
testcase_11 AC 896 ms
10,112 KB
testcase_12 AC 901 ms
10,112 KB
testcase_13 AC 857 ms
9,984 KB
testcase_14 AC 907 ms
9,984 KB
testcase_15 AC 469 ms
9,984 KB
testcase_16 AC 809 ms
9,984 KB
testcase_17 AC 467 ms
9,856 KB
testcase_18 AC 606 ms
9,856 KB
testcase_19 AC 758 ms
9,984 KB
testcase_20 AC 278 ms
6,784 KB
testcase_21 AC 372 ms
6,656 KB
testcase_22 AC 741 ms
5,632 KB
testcase_23 AC 885 ms
7,168 KB
testcase_24 AC 1,129 ms
7,168 KB
testcase_25 AC 186 ms
9,216 KB
testcase_26 AC 135 ms
6,784 KB
testcase_27 AC 620 ms
5,504 KB
testcase_28 AC 575 ms
9,472 KB
testcase_29 AC 1,016 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1008.cc:  No.1008 Bench Craftsman - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int INF = 1 << 30;

/* typedef */

typedef long long ll;

struct Elm { // a+bx
  ll a, b;
  Elm() {}
  Elm(ll _a, ll _b): a(_a), b(_b) {}

  bool operator==(const Elm &e) const { return a == e.a && b == e.b; }
  bool operator!=(const Elm &e) const { return a != e.a || b != e.b; }

  Elm operator+(const Elm &e) const { return Elm(a + e.a, b + e.b); }

  Elm &operator+=(const Elm &e) {
    a += e.a, b += e.b;
    return *this;
  }

  Elm operator*(const int x) const { return Elm(a + b * x, b); }
};

template <typename T, const int MAX_E2>
struct SegTreeSumDelay {
  int e2;
  T nodes[MAX_E2], z;
  int ls[MAX_E2];
  SegTreeSumDelay() {}

  void init(int n, T _z) {
    z = _z;
    for (e2 = 1; e2 < n; e2 <<= 1);
    ls[0] = e2;
    for (int j = 0; j < e2 - 1; j++)
      ls[j * 2 + 1] = ls[j * 2 + 2] = (ls[j] >> 1);
    clear();
  }

  void clear() {
    fill(nodes, nodes + e2 * 2, z);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void __update(int k) {
    if (nodes[k] != z) {
      int k0 = k * 2 + 1, k1 = k0 + 1;
      nodes[k0] += nodes[k];
      nodes[k1] += nodes[k] * ls[k0];
      nodes[k] = z;
    }
  }

  void updateall() {
    for (int j = 0; j < e2 - 1; j++) __update(j);
  }
  
  void add_range(int r0, int r1, T v, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] += v * (i0 - r0);
      return;
    }

    __update(k);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    add_range(r0, r1, v, k0, i0, im);
    add_range(r0, r1, v, k1, im, i1);
  }
  void add_range(int r0, int r1, T v) {
    //printf(" add_range(%d,%d,(%lld,%lld))\n", r0, r1, v.a, v.b);
    add_range(r0, r1, v, 0, 0, e2);
  }
};

/* global variables */

int as[MAX_N], xis[MAX_M], wis[MAX_M];
SegTreeSumDelay<Elm,MAX_E2> st;

/* subroutines */

bool check(int n, int m, int c) {
  st.clear();

  for (int i = 0; i < m; i++) {
    int xi = xis[i], wi = wis[i];
    int l = (wi - 1) / c;

    if (l == 0)
      st.add_range(xi, xi + 1, Elm(wi, 0));
    else {
      st.add_range(xi - l, xi + 1, Elm(wi - c * l, c));
      st.add_range(xi + 1, xi + l + 1, Elm(wi - c, -c));
    }
  }
  st.updateall();

  //printf("c=%d:", c);
  //for (int i = 0; i < n; i++) printf(" %lld", st.get(i).a); putchar('\n');
  for (int i = 0; i < n; i++)
    if (as[i] <= st.get(i).a) return false;
  return true;
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  int mina = INF;
  for (int i = 0; i < n; i++) {
    scanf("%d", as + i);
    if (mina > as[i]) mina = as[i];
  }

  int maxw = 0;
  ll wsum = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", xis + i, wis + i);
    xis[i]--;
    if (as[xis[i]] <= wis[i]) {
      puts("-1");
      return 0;
    }

    if (maxw < wis[i]) maxw = wis[i];
    wsum += wis[i];
  }
  if (mina > wsum) {
    puts("0");
    return 0;
  }
  //printf("mina=%d, maxw=%d, wsum=%lld\n", mina, maxw, wsum);

  st.init(n, Elm(0, 0));

  int c0 = 0, c1 = maxw + 1;
  while (c0 + 1 < c1) {
    int c = (c0 + c1) / 2;
    if (check(n, m, c)) c1 = c;
    else c0 = c;
  }

  printf("%d\n", (c1 > maxw) ? -1 : c1);
  return 0;
}
0