結果
| 問題 |
No.1008 Bench Craftsman
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-01 03:56:57 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,116 ms / 2,000 ms |
| コード長 | 1,628 bytes |
| コンパイル時間 | 2,831 ms |
| コンパイル使用メモリ | 81,424 KB |
| 実行使用メモリ | 73,112 KB |
| 最終ジャッジ日時 | 2024-06-25 20:12:15 |
| 合計ジャッジ時間 | 25,913 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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();
}
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));
}
}