結果

問題 No.1008 Bench Craftsman
ユーザー 37zigen37zigen
提出日時 2020-04-01 03:51:13
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,824 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 75,952 KB
実行使用メモリ 75,284 KB
最終ジャッジ日時 2023-09-08 02:55:28
合計ジャッジ時間 8,824 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
60,752 KB
testcase_01 AC 125 ms
56,276 KB
testcase_02 AC 121 ms
55,552 KB
testcase_03 AC 127 ms
56,160 KB
testcase_04 AC 126 ms
55,772 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long MOD=(long)1e9+7;
	
	boolean f(long[] A,int[] x,long[] w,long c) {
		int N=A.length,M=x.length;
		boolean ret=true;
		if(c==0) {
			long sum=Arrays.stream(w).sum();
			return sum<Arrays.stream(A).min().getAsLong();
		}
		long[] v=new long[N+1];
		int[] L=new int[N];
		int[] R=new int[N];
		for(int i=0;i<M;++i) {
			//w-|i-x|c=0 ⇔ i=x±w/c
			//(-\infty,floor(x-w/c)], [ceil(x+w/c),\infty]
			L[i]=Math.max(-1,(int)(x[i]-(w[i]+c-1)/c));
			R[i]=Math.min(+N,(int)(x[i]+(w[i]+c-1)/c));
			v[L[i]+1]+=c;
			v[x[i]+1]-=2*c;
			v[R[i]]+=c;
		}
		for(int i=1;i<=N;++i) {
			v[i]+=v[i-1];
		}
		for(int i=0;i<M;++i) {// w-|q-x|*c
			v[L[i]+1]+=(w[i]-Math.abs(L[i]+1-x[i])*c)-c;
			v[R[i]]-=(w[i]-Math.abs(R[i]-1-x[i])*c);
		}
		for(int i=1;i<=N;++i) {
			v[i]+=v[i-1];
		}
		for(int i=0;i<N;++i)ret&=v[i]<A[i];
		long[] b=new long[N];
		for(int i=0;i<N;++i) {
			for(int j=0;j<M;++j) {
				b[i]+=Math.max(0, w[j]-Math.abs(i-x[j])*c);
			}
		}
		for(int i=0;i<N;++i)if(b[i]!=v[i])throw new AssertionError();
		return ret;
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		long[] A=new long[N];
		for(int i=0;i<N;++i)A[i]=Long.parseLong(sc.next());
		int[] x=new int[M];
		long[] w=new long[M];
		for(int i=0;i<M;++i) {
			x[i]=Integer.parseInt(sc.next())-1;
			w[i]=Long.parseLong(sc.next());
		}
		long INF=(long)1e6;
		long ok=INF;
		long ng=-1;
		while(ok-ng>1) {
			long middle=(ok+ng)/2;
			if(f(A,x,w,middle)) {
				ok=middle;
			}else {
				ng=middle;
			}
		}
		System.out.println(ok==INF?-1:ok);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0