-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChocolate
More file actions
100 lines (86 loc) · 3.47 KB
/
Chocolate
File metadata and controls
100 lines (86 loc) · 3.47 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
import java.util.*;
public class Chocolate {
static class Rectangle {
int x1, y1, x2, y2;
Rectangle(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
int area() {
return (x2 - x1) * (y2 - y1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int W = sc.nextInt();
int H = sc.nextInt();
int n = sc.nextInt();
// Read breaks
int[][] breaks = new int[n][4];
for (int i = 0; i < n; i++) {
breaks[i][0] = sc.nextInt();
breaks[i][1] = sc.nextInt();
breaks[i][2] = sc.nextInt();
breaks[i][3] = sc.nextInt();
}
// Initial rectangle
List<Rectangle> rects = new ArrayList<>();
rects.add(new Rectangle(0, 0, W, H));
// Process breaks in any order - we need to find a valid order
// Since n <= 100, we can try to find which break to process next
boolean[] processed = new boolean[n];
int processedCount = 0;
while (processedCount < n) {
for (int i = 0; i < n; i++) {
if (processed[i]) continue;
int x1 = breaks[i][0], y1 = breaks[i][1];
int x2 = breaks[i][2], y2 = breaks[i][3];
// Check if this break can be applied to any rectangle
for (int j = 0; j < rects.size(); j++) {
Rectangle r = rects.get(j);
if (x1 == x2) { // vertical break
if (x1 > r.x1 && x1 < r.x2 && y1 == r.y1 && y2 == r.y2) {
// Split rectangle
Rectangle left = new Rectangle(r.x1, r.y1, x1, r.y2);
Rectangle right = new Rectangle(x1, r.y1, r.x2, r.y2);
rects.remove(j);
rects.add(left);
rects.add(right);
processed[i] = true;
processedCount++;
break;
}
} else if (y1 == y2) { // horizontal break
if (y1 > r.y1 && y1 < r.y2 && x1 == r.x1 && x2 == r.x2) {
// Split rectangle
Rectangle bottom = new Rectangle(r.x1, r.y1, r.x2, y1);
Rectangle top = new Rectangle(r.x1, y1, r.x2, r.y2);
rects.remove(j);
rects.add(bottom);
rects.add(top);
processed[i] = true;
processedCount++;
break;
}
}
}
if (processed[i]) break;
}
}
// Collect areas
int[] areas = new int[rects.size()];
for (int i = 0; i < rects.size(); i++) {
areas[i] = rects.get(i).area();
}
// Sort in increasing order
Arrays.sort(areas);
// Output
for (int i = 0; i < areas.length; i++) {
System.out.print(areas[i]);
if (i < areas.length - 1) System.out.print(" ");
}
System.out.println();
}
}