-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAllergyTest.java
79 lines (62 loc) · 2.41 KB
/
AllergyTest.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
/*
Problem: https://open.kattis.com/problems/allergy
Author: Adrian Reithaug
Submitted: April 23rd, 2017
Time: 0.35s / 3.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AllergyTest {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int totalAllergens = Integer.parseInt(reader.readLine());
int[] numAllergens = new int[8];
numAllergens[0] = 1;
for (int i = 0; i < totalAllergens; i++) {
numAllergens[Integer.parseInt(reader.readLine())]++;
}
Map<Integer, Integer> storedDurations = new HashMap<>();
System.out.println(testScheme(numAllergens, storedDurations));
}
static int testScheme(int[] numAllergens, Map<Integer, Integer> storedDurations) {
int hashCode = Arrays.hashCode(numAllergens);
if (storedDurations.get(hashCode) != null) {
return storedDurations.get(hashCode);
}
List<Integer> allergens = new ArrayList<>();
int days = numAllergens[0];
int totalAllergens = 0;
for (int i = 1; i < 8; i++) {
totalAllergens += numAllergens[i];
if (numAllergens[i] != 0) {
allergens.add(i);
}
}
if (totalAllergens == 1) {
return (days == allergens.get(0)) ? days : Integer.MAX_VALUE;
}
int shortestScheme = Integer.MAX_VALUE;
for (int i = 0; i < allergens.size(); i++) {
int liveDuration = allergens.get(i);
if (liveDuration >= days) {
for (int j = 1; j < 8; j++) {
if (liveDuration < days + j) {
numAllergens[0] = j;
numAllergens[liveDuration]--;
int thisScheme = testScheme(numAllergens, storedDurations) + days;
numAllergens[liveDuration]++;
shortestScheme = (thisScheme > 0) ? Math.min(shortestScheme, thisScheme) : shortestScheme;
}
}
}
}
storedDurations.put(hashCode, shortestScheme);
return shortestScheme;
}
}