-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInDeCopy.java
More file actions
56 lines (47 loc) · 1.38 KB
/
Copy pathInDeCopy.java
File metadata and controls
56 lines (47 loc) · 1.38 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
43
44
45
46
47
48
49
50
51
52
53
54
55
package PracJava;
import java.util.Scanner;
public class InDeCopy {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int g = 0; g<t; g++){
int n = input.nextInt();
int[] a = new int[n];
int[] b = new int[n+1];
for(int i = 0; i<n; i++){
a[i]=input.nextInt();
}
for(int j = 0; j<=n;j++){
b[j] =input.nextInt();
}
System.out.println(minOp(n, a, b));
}
}
public static long minOp(int n, int[] a, int[] b){
long op = 0;
int last = b[n];
boolean boo1 = false;
int minDif = Math.abs(a[0]-last);
for(int i = 0; i<n; i++){
op+=(Math.abs(a[i]-b[i]));
if(!boo1){
if((a[i]>=last&&b[i]<=last)||(a[i]<=last&&b[i]>=last)){
boo1 = true;
}else{
if(minDif>Math.abs(a[i]-last)){
minDif = Math.abs(a[i]-last);
}
if(minDif>Math.abs(b[i]-last)){
minDif = Math.abs(b[i]-last);
}
}
}
}
if(boo1){
op+=1;
}else{
op+=(minDif+1);
}
return op;
}
}