結果
| 問題 | 
                            No.1008 Bench Craftsman
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-04-01 03:59:13 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,248 ms / 2,000 ms | 
| コード長 | 1,597 bytes | 
| コンパイル時間 | 3,162 ms | 
| コンパイル使用メモリ | 81,028 KB | 
| 実行使用メモリ | 73,008 KB | 
| 最終ジャッジ日時 | 2024-06-25 20:17:13 | 
| 合計ジャッジ時間 | 28,286 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	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));
	}
}