-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFJActuallyFarms.java
More file actions
42 lines (40 loc) · 1.79 KB
/
Copy pathFJActuallyFarms.java
File metadata and controls
42 lines (40 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
import java.util.*;
public class FJActuallyFarms {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t -- > 0) {
int n = Integer.parseInt(br.readLine());
int[] h = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] p = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] ordByGoalHeight = new int[n];
for(int i=0; i<n; i++) ordByGoalHeight[i] = i;
ordByGoalHeight = Arrays.stream(ordByGoalHeight).boxed().sorted((i, j) -> p[j] - p[i]).mapToInt(i -> i).toArray();
int ret = 0;
for(int i=0; i+1<n; i++) {
int smaller = ordByGoalHeight[i];
int larger = ordByGoalHeight[i+1];
if(h[smaller] >= h[larger] && a[smaller] < a[larger]) {
ret = Math.max(ret, ceilingDiv(h[smaller] - h[larger] + 1, a[larger] - a[smaller]));
}
}
int[] trueHeights = new int[n];
for(int i=0; i<n; i++) {
trueHeights[i] = h[i] + a[i] * ret;
}
for(int i=0; i+1<n; i++) {
int smaller = ordByGoalHeight[i];
int larger = ordByGoalHeight[i+1];
if(trueHeights[smaller] >= trueHeights[larger]) ret = -1;
}
pw.println(ret);
}
pw.close();
}
static int ceilingDiv(int a, int b) {
return (a+b-1)/b;
}
}