forked from alec121212/AlgorithmsAndAbstractDesign
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram4.java
More file actions
Latest commit
80 lines (72 loc) · 2.63 KB
/
Copy pathProgram4.java
File metadata and controls
80 lines (72 loc) · 2.63 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
classProgram4 {
publicrecordResult(intnumPlatforms, inttotalHeight, int[] numPaintings) {}
/**
* Solution to program 4
*
* @param n number of paintings
* @param w width of the platform
* @param heights array of heights of the paintings
* @param widths array of widths of the paintings
* @return Result object containing the number of platforms, total height of the paintings,
* and the number of paintings on each platform
*/
privatestaticResultprogram4(intn, intw, int[] heights, int[] widths) {
int[] minHeight = newint[n + 1];
int[] split = newint[n + 1];
minHeight[0] = 0;
for (inti = 1; i <= n; i++) {
minHeight[i] = Integer.MAX_VALUE;
for (intj = 0; j < i; j++) {
inttotalWidth = 0;
intmaxHeight = 0;
for (intk = j; k < i; k++) {
totalWidth += widths[k];
maxHeight = Math.max(maxHeight, heights[k]);
}
if (totalWidth <= w) {
inttotalHeight = minHeight[j] + maxHeight;
if (totalHeight < minHeight[i]) {
minHeight[i] = totalHeight;
split[i] = j;
}
}
}
}
List<Integer> paintings = newArrayList<>();
intindex = n;
while (index > 0) {
intstart = split[index];
paintings.add(0, index - start);
index = start;
}
intnumPlatforms = paintings.size();
int[] numPaintings = newint[paintings.size()];
for (inti = 0; i < paintings.size(); i++) {
numPaintings[i] = paintings.get(i);
}
returnnewResult(numPlatforms, minHeight[n], numPaintings);
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
intW = sc.nextInt();
int[] heights = newint[n];
int[] widths = newint[n];
for (inti = 0; i < n; i++) {
heights[i] = sc.nextInt();
}
for (inti = 0; i < n; i++) {
widths[i] = sc.nextInt();
}
sc.close();
Resultresult = program4(n, W, heights, widths);
System.out.println(result.numPlatforms());
System.out.println(result.totalHeight());
for (inti = 0; i < result.numPaintings().length; i++) {
System.out.println(result.numPaintings()[i]);
}
}
}