結果

問題 No.1008 Bench Craftsman
ユーザー nebukuro09nebukuro09
提出日時 2020-03-06 22:26:16
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 1,477 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 125,376 KB
実行使用メモリ 29,076 KB
最終ジャッジ日時 2024-06-22 05:40:47
合計ジャッジ時間 4,170 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(to!long).array;
    auto XW = iota(M).map!(_ => readln.split.map!(to!int).array).array;

    auto B = new long[](N);
    foreach (xw; XW) B[xw[0]-1] += xw[1];

    if (N.iota.map!(i => A[i] <= B[i]).any) {
        writeln(-1);
        return;
    }

    long[] calc(int[] X, long[] Y) {
        auto LtoR = new long[](N);
        auto RtoL = new long[](N);

        foreach (i; 0..M) {
            auto idx = X[i];
            if (idx < N - 1) LtoR[idx+1] += Y[i];
            if (idx > 0) RtoL[idx-1] += Y[i];
        }

        foreach (i; 0..N-1) LtoR[i+1] += LtoR[i];
        foreach (i; 0..N-1) LtoR[i+1] += LtoR[i];
        foreach_reverse (i; 1..N) RtoL[i-1] += RtoL[i];
        foreach_reverse (i; 1..N) RtoL[i-1] += RtoL[i];

        return N.iota.map!(i => LtoR[i] + RtoL[i]).array;
    }

    auto X = XW.map!(xw => xw[0].to!int-1).array;
    auto Y1 = new long[](M); Y1[] = 1;
    auto Y2 = XW.map!(xw => xw[1].to!long).array;
    auto C = calc(X, Y1);
    auto D = calc(X, Y2);

    auto S = Y2.sum;
    auto ans = 0L;

    foreach (i, a; A) if (a <= S) {
        ans = max(ans, (S - a) / C[i] + ((S - a) % C[i] > 0));
    }

    ans.writeln;
}
0