Skip to content

Commit 0cf04bd

Browse files
authored
Update Main2.java
It's mine!
1 parent be0abbe commit 0cf04bd

File tree

1 file changed

+60
-43
lines changed

1 file changed

+60
-43
lines changed

Kattis/Muzicary/Main2.java

+60-43
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
1+
import java.util.ArrayList;
12
import java.util.Scanner;
23

3-
public class Main2 {
4+
public class Main3 {
5+
private static int T, N;
6+
private static int[] weigh;
7+
private static X[][] memo;
48

59
public static void main(String[] args) {
610
Scanner scanner = new Scanner(System.in);
7-
int T = scanner.nextInt();
8-
int N = scanner.nextInt();
9-
int arr[] = new int[N];
10-
int memo[][] = new int[N + 1][T + 1];
11+
T = scanner.nextInt();
12+
N = scanner.nextInt();
13+
weigh = new int[N + 1];
14+
memo = new X[N + 1][T + 1];
1115
for (int i = 0; i < N; i++) {
12-
arr[i] = scanner.nextInt();
13-
}
14-
// We solve it like a knapsack problem, but assume any empty space is negative and
15-
// we're gonna fill it as much as possible,thus in best position,
16-
// memo[N][T] will be zero,weight of any item is length of rest
17-
// value of any item is again length,so any item val/weight = 1 and we're only want to fill it
18-
for (int i = 0; i <= T; i++) {
19-
memo[0][i] = -i;
20-
}
21-
int mark[][] = new int[N + 1][T + 1];
22-
for (int i = 1; i <= N; i++) {
23-
for (int j = 0; j <= T; j++) {
24-
if (arr[i - 1] <= j && memo[i - 1][j] - 1 < memo[i][j - arr[i - 1]]) {
25-
memo[i][j] = memo[i - 1][j - arr[i - 1]];
26-
mark[i][j] = 1;
27-
// System.out.println(i + ",J:" + j);
28-
}else{
29-
memo[i][j] = memo[i - 1][j] - 1;
30-
}
31-
}
32-
}
33-
int j = T;
34-
int belong[] = new int[N + 1];
35-
/*Problem has two lines of rest
36-
we have to calculate which one of musicians can rest in the first line,
37-
rest belong to second line:
38-
*/
39-
for (int i = N; j > 0 && i > 0 ; i--) {
40-
if (mark[i][j] == 1){
41-
j -= arr[i - 1];
42-
belong[i - 1] = 1;
43-
}
16+
weigh[i] = scanner.nextInt();
4417
}
45-
//We're sure any musician that doesn't belong to first line belong to second:
46-
int one = 0 , two = 0;
18+
X res = func(0, T, new ArrayList<>());
19+
// System.out.println(res.arrayList);
20+
int one = 0, two = 0;
4721
for (int i = 0; i < N; i++) {
48-
if (belong[i] == 1){
22+
if (res.arrayList != null && res.arrayList.contains(i)) {
4923
System.out.print(one + " ");
50-
one += arr[i];
51-
}else{
24+
one += weigh[i];
25+
} else {
5226
System.out.print(two + " ");
53-
two += arr[i];
27+
two += weigh[i];
5428
}
5529
}
30+
}
5631

32+
private static X func(int id, int rem, ArrayList<Integer> arrayList) {
33+
if (rem < 0) {
34+
return new X(arrayList, Integer.MIN_VALUE);
35+
}
36+
if (id == N) {
37+
return new X(arrayList, -rem);
38+
}
39+
if (weigh[id] > rem) {
40+
return func(id + 1, rem, arrayList);
41+
}
42+
if (memo[id][rem] != null) {
43+
return memo[id][rem];
44+
}
45+
ArrayList<Integer> arrayList1 = new ArrayList<>();
46+
arrayList1.add(id);
47+
arrayList1.addAll(arrayList);
48+
X x1 = func(id + 1, rem - weigh[id], arrayList1);
49+
X x2 = func(id + 1, rem, arrayList);
50+
if (x1.res >= x2.res) {
51+
memo[id][rem] = x1;
52+
} else {
53+
memo[id][rem] = x2;
54+
}
55+
return memo[id][rem];
56+
}
57+
58+
private static class X {
59+
ArrayList<Integer> arrayList = new ArrayList<>();
60+
int res = 0;
61+
62+
X(ArrayList<Integer> arrayList, int res) {
63+
this.arrayList = arrayList;
64+
this.res = res;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return "X{" +
70+
"arrayList=" + arrayList +
71+
", res=" + res +
72+
'}';
73+
}
5774
}
5875
}

0 commit comments

Comments
 (0)