-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclosingtheloop.java
More file actions
67 lines (58 loc) · 1.21 KB
/
closingtheloop.java
File metadata and controls
67 lines (58 loc) · 1.21 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class closingtheloop {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testcases = scan.nextInt();
String str;
for (int zaxbys = 0; zaxbys < testcases; zaxbys++)
{
ArrayList<Integer> reds = new ArrayList<>();
ArrayList<Integer> blues = new ArrayList<>();
int ropes = scan.nextInt();
for (int i = 0; i < ropes; i++)
{
str = scan.next();
if (str.endsWith("B"))
blues.add(Integer.parseInt(str.substring(0, str.length() - 1)));
else
reds.add(Integer.parseInt(str.substring(0, str.length() - 1)));
}
Collections.sort(reds);
Collections.sort(blues);
if (reds.isEmpty() || blues.isEmpty())
System.out.println("0");
else
{
int sum = 0;
int rope = 0;
boolean color = true;
while (true)
{
rope = 0;
if (color)
{
if (!reds.isEmpty())
rope = reds.remove(reds.size() - 1);
}
else
{
if (!blues.isEmpty())
rope = blues.remove(blues.size() - 1);
}
if (rope == 0)
break;
color = !color;
System.out.println(rope);
sum += rope - 1;
}
if (color)
System.out.println(sum - rope);
else
System.out.println(sum);
}
}
scan.close();
}
}