-
Notifications
You must be signed in to change notification settings - Fork 6
/
SolutionNetworkCabling.txt
55 lines (41 loc) · 1.39 KB
/
SolutionNetworkCabling.txt
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
// Read inputs from System.in, Write outputs to System.out.
// Your class name has to be Solution
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<Integer> x = new ArrayList<>();
List<Integer> y = new ArrayList<>();
for (int i = 0; i < n; ++i) {
x.add(in.nextInt());
y.add(in.nextInt());
}
int minX = Collections.min(x);
int maxX = Collections.max(x);
Collections.sort(y);
int mediane = median(y);
Long longueur = longueurTotale(y,mediane,maxX,minX);
System.out.println(longueur);
}
public static long longueurTotale(List<Integer> y, Integer mediane, Integer maxX,Integer minX ) {
long longueurT = 0l;
long longueur = 0l;
for(int i=0; i<y.size() ; i++){
longueur = Math.abs(y.get(i).intValue()-mediane.intValue());
longueurT = longueurT+ longueur;
}
longueurT = longueurT + Math.abs(maxX-minX);
return longueurT;
}
public static int median(List<Integer> m) {
int middle = m.size()/2;
if (m.size()%2 == 1) {
return m.get(middle);
} else {
return (m.get(middle-1) + m.get(middle)) / 2;
}
}
}