結果

問題 No.1008 Bench Craftsman
ユーザー startcppstartcpp
提出日時 2020-03-06 23:28:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 1,832 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 67,300 KB
実行使用メモリ 8,392 KB
最終ジャッジ日時 2024-04-22 10:38:27
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
7,628 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,948 KB
testcase_05 AC 162 ms
8,260 KB
testcase_06 AC 59 ms
7,956 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 10 ms
6,948 KB
testcase_09 AC 82 ms
6,944 KB
testcase_10 AC 147 ms
8,268 KB
testcase_11 AC 150 ms
8,208 KB
testcase_12 AC 148 ms
8,372 KB
testcase_13 AC 150 ms
8,260 KB
testcase_14 AC 151 ms
8,392 KB
testcase_15 AC 133 ms
8,132 KB
testcase_16 AC 144 ms
8,136 KB
testcase_17 AC 135 ms
8,260 KB
testcase_18 AC 151 ms
8,136 KB
testcase_19 AC 145 ms
8,268 KB
testcase_20 AC 56 ms
6,940 KB
testcase_21 AC 56 ms
6,944 KB
testcase_22 AC 88 ms
7,244 KB
testcase_23 AC 118 ms
7,236 KB
testcase_24 AC 109 ms
8,140 KB
testcase_25 AC 56 ms
6,944 KB
testcase_26 AC 44 ms
7,700 KB
testcase_27 AC 72 ms
6,984 KB
testcase_28 AC 87 ms
7,108 KB
testcase_29 AC 129 ms
8,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//やりたいことは何となく分かるけど、詰めるのが難しい。
#include <iostream>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, m;
int a[100000];
int x[100000], w[100000];

//区間[l, r)にa, a+d, a+2d, …を足す
//・s1[]では, a, a, a…の足し算を担当(1回階差)
//・s2[]では, 0, d, 2d…の足し算を担当(2回階差)
void add(int s1[], int s2[], int l, int r, int a, int d) {
	//cout << "l = " << l << ", r = " << r << ", a = " << a << ", d = " << d << endl;
	if (r - l <= 0) { return; }
	s1[l] += a;
	s1[r] -= a;
	
	if (r - l <= 1) { return; }
	s2[l + 1] += d;
	s2[r]     -= (r - l) * d;
	s2[r + 1] += (r - l - 1) * d;
}

bool check(int c) {
	int i;
	static int s1[100001];
	static int s2[100002];
	static int s[100000];
	
	rep(i, n) s1[i] = 0;
	rep(i, n) s2[i] = 0;
	rep(i, n) s[i] = 0;
	
	if (c == 0) {
		int sw = 0;
		rep(i, m) sw += w[i];
		rep(i, n) if (sw >= a[i]) return false;
		return true;
	}
	
	rep(i, m) {
		int l = x[i] - (w[i] + c - 1) / c + 1; if (l < 0) l = 0;
		int r = x[i] + (w[i] + c - 1) / c; if (r > n) r = n;
		add(s1, s2, l, x[i], w[i] - c * (x[i] - l), c);
		add(s1, s2, x[i], r, w[i], -c);
	}
	
	rep(i, n) s1[i + 1] += s1[i];
	rep(i, n) s2[i + 1] += s2[i];
	rep(i, n) s2[i + 1] += s2[i];
	rep(i, n) s[i] = s1[i] + s2[i];
	
	//rep(i, n) cout << s1[i] << " "; cout << endl;
	//rep(i, n) cout << s2[i] << " "; cout << endl;
	
	rep(i, n) { if (s[i] >= a[i]) return false; }
	return true;
}

signed main() {
	int i;
	
	cin >> n >> m;
	rep(i, n) { cin >> a[i]; }
	rep(i, m) { cin >> x[i] >> w[i]; x[i]--; }
	
	int ng = -1, ok = 114514, mid;
	while (ok - ng >= 2) {
		mid = (ok + ng) / 2;
		if (check(mid)) ok = mid;
		else ng = mid;
	}
	if (ok >= 114514) cout << "-1" << endl;
	else cout << ok << endl;
	return 0;
}
0