- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram2.java
More file actions
Latest commit
119 lines (108 loc) · 3.94 KB
/
Copy pathProgram2.java
File metadata and controls
119 lines (108 loc) · 3.94 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
importjava.util.Scanner;
importjava.util.ArrayList;
importjava.util.List;
classProgram2 {
publicrecordResult(intnumPlatforms, inttotalHeight, int[] numPaintings) {}
/**
* Solution to program 2
*
* @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
*/
staticResultprogram2(intn, intW, int[] heights, int[] widths) {
inttotalHeight = 0;
intnumPlatforms = 0;
List<Integer> paintingsPerPlat = newArrayList<>();
// Find the index k where heights start increasing
intk = n; // Default k is n, meaning all heights are non-increasing
for (inti = 1; i < n; i++) {
if (heights[i] > heights[i - 1]) {
k = i;
break;
}
}
// Process descending part (from index 0 to k-1)
intcurrWidth = 0;
intstartIndex = 0;
intcounter = 0;
for (inti = 0; i < k; i++) {
if (currWidth + widths[i] <= W) {
// Add painting to current platform
currWidth += widths[i];
counter++;
} else {
// Finish current platform
numPlatforms++;
totalHeight += heights[startIndex]; // Tallest painting is the first one
paintingsPerPlat.add(counter);
// Start new platform
currWidth = widths[i];
startIndex = i;
counter = 1;
}
}
// Handle the last platform in descending part
if (counter > 0) {
numPlatforms++;
totalHeight += heights[startIndex];
paintingsPerPlat.add(counter);
}
// Process ascending part (from index k to n-1)
currWidth = 0;
counter = 0;
inttallestHeight = 0;
for (inti = k; i < n; i++) {
if (currWidth + widths[i] <= W) {
// Add painting to current platform
currWidth += widths[i];
counter++;
tallestHeight = heights[i]; // Heights are increasing
} else {
// Finish current platform
numPlatforms++;
totalHeight += tallestHeight; // Tallest painting is the last one
paintingsPerPlat.add(counter);
// Start new platform
currWidth = widths[i];
tallestHeight = heights[i];
counter = 1;
}
}
// Handle the last platform in ascending part
if (counter > 0) {
numPlatforms++;
totalHeight += tallestHeight;
paintingsPerPlat.add(counter);
}
// Convert paintingsPerPlat to int array
int[] numPaintings = newint[paintingsPerPlat.size()];
for (inti = 0; i < numPaintings.length; i++) {
numPaintings[i] = paintingsPerPlat.get(i);
}
returnnewResult(numPlatforms, totalHeight, 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 = program2(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]);
}
}
}