-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay74.java
More file actions
54 lines (54 loc) · 1.45 KB
/
Copy pathDay74.java
File metadata and controls
54 lines (54 loc) · 1.45 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
import java.util.*;
public class Day74{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
int tc = in.nextInt();
for (int i=0;i<tc ;i++ ) {
int total = in.nextInt();
String pairs[] = new String[in.nextInt()];
in.nextLine();
for (int j=0;j<pairs.length ;j++ ) {
pairs[j] = in.nextLine();
}
int index = 0,sum = 0,count=0;
while(true){
boolean trigger = true;
String res = "";
for (int j=index;j<pairs.length ;j++ ) {
if (trigger) {
res+=pairs[j]+" ";
trigger = false;
}else{
if (res.contains(Character.toString(pairs[j].charAt(0)))||
res.contains(Character.toString(pairs[j].charAt(2)))) {
index = j+1;
if (!res.contains(Character.toString(pairs[j].charAt(0)))) {
res+=Character.toString(pairs[j].charAt(0))+" ";
}else if (!res.contains(Character.toString(pairs[j].charAt(2)))) {
res+=Character.toString(pairs[j].charAt(2))+" ";
}
}else{
continue;
}
}
}
res = res.replaceAll("[^0-9]", "");
sum+=res.length();
count++;
pq.add(res.length());
if (index==pairs.length) {
break;
}
}
if (sum!=total) {
pq.add(total-sum);
count++;
}
System.out.println(count);
while(!pq.isEmpty()){
System.out.print(pq.remove()+" ");
}
}
}
}