結果

問題 No.1008 Bench Craftsman
ユーザー 37zigen37zigen
提出日時 2020-04-01 03:56:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,161 ms / 2,000 ms
コード長 1,628 bytes
コンパイル時間 3,466 ms
コンパイル使用メモリ 76,056 KB
実行使用メモリ 74,424 KB
最終ジャッジ日時 2023-09-08 03:03:28
合計ジャッジ時間 27,132 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,108 KB
testcase_01 AC 126 ms
56,016 KB
testcase_02 AC 124 ms
55,692 KB
testcase_03 AC 130 ms
55,776 KB
testcase_04 AC 130 ms
55,952 KB
testcase_05 AC 1,161 ms
69,944 KB
testcase_06 AC 663 ms
61,516 KB
testcase_07 AC 118 ms
56,200 KB
testcase_08 AC 276 ms
60,580 KB
testcase_09 AC 664 ms
63,652 KB
testcase_10 AC 985 ms
67,256 KB
testcase_11 AC 991 ms
69,232 KB
testcase_12 AC 992 ms
69,372 KB
testcase_13 AC 1,024 ms
73,936 KB
testcase_14 AC 1,030 ms
67,300 KB
testcase_15 AC 793 ms
65,396 KB
testcase_16 AC 975 ms
65,464 KB
testcase_17 AC 951 ms
67,484 KB
testcase_18 AC 946 ms
68,128 KB
testcase_19 AC 1,024 ms
67,296 KB
testcase_20 AC 657 ms
63,452 KB
testcase_21 AC 662 ms
59,884 KB
testcase_22 AC 806 ms
69,652 KB
testcase_23 AC 902 ms
69,084 KB
testcase_24 AC 859 ms
70,060 KB
testcase_25 AC 664 ms
61,788 KB
testcase_26 AC 580 ms
61,764 KB
testcase_27 AC 725 ms
66,760 KB
testcase_28 AC 806 ms
67,536 KB
testcase_29 AC 940 ms
74,424 KB
権限があれば一括ダウンロードができます

ソースコード

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[M];
		int[] R=new int[M];
		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];
		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