forked from sherxon/AlgoDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice2.java
More file actions
59 lines (55 loc) · 1.79 KB
/
Copy pathPractice2.java
File metadata and controls
59 lines (55 loc) · 1.79 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
package codejam2018;
import java.util.Scanner;
/**
* Why Did you create this class? what does it do?
*/
public class Practice2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < t; i++) {
builder.append("Case #").append(i + 1).append(": ");
int n = in.nextInt();
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = in.nextInt();
}
int max, max2, maxi, maxi2;
while (true) {
long sum = 0;
max = max2 = maxi = maxi2 = -1;
for (int j = 0; j < a.length; j++) {
if (a[j] > max) {
maxi2 = maxi;
max2 = max;
max = a[j];
maxi = j;
} else if (a[j] > max2) {
max2 = a[j];
maxi2 = j;
}
sum += a[j];
}
if (max == 0)
break;
a[maxi]--;
builder.append((char) (maxi + 'A'));
if (sum == 3 && max == 1) {
builder.append(" ");
continue;
}
if (max > max2) {
a[maxi]--;
builder.append((char) (maxi + 'A'));
} else if (maxi2 != -1) {
a[maxi2]--;
builder.append((char) (maxi2 + 'A'));
}
builder.append(" ");
}
builder.append("\n");
}
System.out.println(builder.toString());
}
}