Skip to content

Commit 2fb21c4

Browse files
committed
tshirts right answer
1 parent aec08ec commit 2fb21c4

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/main/java/main/java/videos/TShirts.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ public static void main(String[] args) throws IOException {
1111
final StringBuilder stringBuilder = new StringBuilder();
1212
for (int t = Integer.parseInt(br.readLine()); t > 0; t--) {
1313
final int n = Integer.parseInt(br.readLine());
14-
final int sets[] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
15-
stringBuilder.append(new Solver(sets).solve()).append('\n');
14+
final int wardrobe[][] = new int[n][];
15+
for (int i = 0; i < n; i++) {
16+
wardrobe[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
17+
}
18+
final Solver solver = new Solver(wardrobe);
19+
stringBuilder.append(solver.solve()).append('\n');
1620
}
1721
System.out.println(stringBuilder);
1822
}
@@ -21,31 +25,50 @@ public static void main(String[] args) throws IOException {
2125
class Solver {
2226

2327
public static final int MOD = 1000000007;
24-
private int[][] shirtSets;
28+
private final int length;
29+
private int[][] people;
30+
private final int[] count;
31+
private final long dp[][];
2532

26-
public Solver(final int[] people) {
27-
final int shirtSets[][] = new int[100][people.length];
28-
final int count[] = new int[people.length];
29-
for (int i = 0; i < people.length; i++) {
30-
shirtSets[people[i]][count[people[i]]++] = i;
33+
public Solver(final int[][] wardrobe) {
34+
final int people[][] = new int[101][wardrobe.length];
35+
count = new int[101];
36+
for (int i = 0; i < wardrobe.length; i++) {
37+
for (int index = 0; index < wardrobe[i].length; index++) {
38+
int shirt = wardrobe[i][index];
39+
people[shirt][count[shirt]++] = i;
40+
}
41+
}
42+
this.length = wardrobe.length;
43+
this.people = people;
44+
dp = new long[1 << length][101];
45+
for (final long[] aDp : dp) {
46+
Arrays.fill(aDp, -1);
3147
}
32-
this.shirtSets = shirtSets;
3348
}
3449

3550
public long solve() {
3651
return possiblities(0, 0);
3752
}
3853

3954
private long possiblities(final int people, final int assigned) {
55+
if (assigned == this.people.length) {
56+
return people == (1 << length) - 1 ? 1 : 0;
57+
}
58+
if (dp[people][assigned] != -1) {
59+
return dp[people][assigned];
60+
}
4061
long answer = 0;
41-
for (int i = 0; i < shirtSets[assigned].length; i++) {
42-
int person = shirtSets[assigned][i];
62+
for (int i = 0; i < count[assigned]; i++) {
63+
int person = this.people[assigned][i];
4364
if ((people & (1 << person)) == 0) {
4465
int changedPeople = people;
4566
changedPeople = changedPeople | (1 << person);
4667
answer = (answer + possiblities(changedPeople, assigned + 1)) % MOD;
4768
}
4869
}
70+
answer = (answer + possiblities(people, assigned + 1)) % MOD;
71+
dp[people][assigned] = answer;
4972
return answer;
5073
}
5174
}

0 commit comments

Comments
 (0)