-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSavingTheUniverseAgain.java
92 lines (79 loc) · 2.95 KB
/
SavingTheUniverseAgain.java
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
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
/**
* https://codejam.withgoogle.com/2018/challenges/00000000000000cb/dashboard
*/
public class SavingTheUniverse {
private static int TotalImpact(String str) {
if (str.length() <= 0 || str.equals("")) {
return 0;
}
int currentChargeStrength = 1;
int impact = 0;
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == 'C') {
currentChargeStrength *= 2;
} else if (str.charAt(i) == 'S'){
impact += currentChargeStrength;
}
}
return impact;
}
private static int CountCharOccurance(String str, char c) {
if (str.length() <= 0 || str.equals("")) {
return 0;
}
int count = str.length() - str.replace(String.valueOf(c), "").length();
return count;
}
private static String MinimumHacks(String str, int shieldStrength) {
String result = "";
int impact = TotalImpact(str);
int c_count = CountCharOccurance(str, 'C');
int s_count = CountCharOccurance(str, 'S');
if (s_count > shieldStrength) {
result = "IMPOSSIBLE";
} else if (c_count == str.length()) {
result = String.valueOf(0);
} else if (s_count == str.length()) {
result = String.valueOf(0);
} else if (impact <= shieldStrength) {
result = String.valueOf(0);
} else {
Map<Integer, Integer> chargeHashMap = new HashMap<>();
int countCharge = 0;
for(int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'C') {
countCharge++;
chargeHashMap.put(countCharge, i);
}
}
int damageDifference = impact - shieldStrength;
int numSwaps = 0;
for(int i = countCharge; i > 0; i--) {
int swapsNeeded = (int) Math.ceil(damageDifference / Math.pow(2, i - 1));
int possibleSwaps = (str.length() - 1) - (countCharge - i) - chargeHashMap.get(i);
if (swapsNeeded > possibleSwaps) {
numSwaps += possibleSwaps;
damageDifference -= Math.pow(2, i - 1) * possibleSwaps;
} else {
numSwaps += swapsNeeded;
break;
}
}
result = String.valueOf(numSwaps);
}
return result;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int i = 0; i < T; ++i) {
int D = in.nextInt();
String P = in.next();
String result = MinimumHacks(P, D);
System.out.println("Case #" + (i + 1) + ": " + result);
}
}
}