-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva1235.java
More file actions
119 lines (109 loc) · 3.05 KB
/
Uva1235.java
File metadata and controls
119 lines (109 loc) · 3.05 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.*;
import java.io.*;
/*
* 1235 - Anti Brute Force Lock
* @ author Mostafa Kamel
*/
public class Uva1235
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader br = new BufferedReader(new FileReader("in.txt"));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while (t-->0)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int [] arr = new int[n];
for(int i = 0 ; i < n ; i++)
arr[i] = Integer.parseInt(st.nextToken());
int mw = 0 ;
int min = 16 ;
for (int i = 0; i < n; i++)
min = Integer.min(min, getWeight(0,arr[i]) );
mw += min ;
int size = n*(n-1)/2 ;
Edge [] edgeList = new Edge[size];
int index = 0 ;
for (int i = 0; i < n ; i++) {
for (int j = i+1 ; j < n ; j++) {
edgeList[index++] = new Edge(i,j,getWeight(arr[i],arr[j]));
}
}
Arrays.sort(edgeList);
UF uf = new UF(n);
for (int i = 0; i < edgeList.length; i++) {
int u = edgeList[i].u;
int v = edgeList[i].v;
int w = edgeList[i].w ;
if(uf.connected(u,v)) continue;
mw += w ;
uf.union(u,v);
}
sb.append(mw).append("\n");
}
pr.print(sb.toString());
br.close();
pr.close();
}
static int getWeight(int m , int n)
{
int w = 0 ;
for (int i = 0; i < 4; i++ , n /= 10 , m /=10) {
int d = Math.abs(m%10-n%10);
w += Integer.min(d,10-d);
}
return w ;
}
static class Edge implements Comparable<Edge>
{
int u , v ,w ;
public Edge(int x , int y , int z)
{
u = x ;
v = y ;
w = z ;
}
@Override
public int compareTo(Edge e)
{
return this.w - e.w ;
}
}
static class UF
{
private int [] id ;
private int count ;
public UF(int N)
{
count = N ;
id = new int[N];
for (int i = 0; i < N; i++)
id[i] = i ;
}
public int count()
{
return count ;
}
public boolean connected(int p , int q)
{
return find(p) == find(q);
}
public int find(int p)
{
return id[p];
}
public void union(int p ,int q)
{
int pID = find(p);
int qID = find(q);
if(qID==pID) return ;
for (int i = 0; i < id.length; i++)
if(id[i]==pID) id[i]=qID;
count-- ;
}
}
}