-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpackrec.java
More file actions
203 lines (178 loc) · 4.14 KB
/
packrec.java
File metadata and controls
203 lines (178 loc) · 4.14 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
/*
ID: mryuan01
LANG: JAVA
TASK: packrec
*/
public class packrec {
public static class Rectangle implements Comparable<Rectangle>{
int h;
int w;
@Override
public int compareTo(Rectangle arg0) {
// TODO Auto-generated method stub
return w - arg0.w;
}
public Rectangle makeStandard(){
if(w > h){
int tmp = w;
w = h;
h = tmp;
}
return this;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * w + h;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
return h == other.h && w == other.w;
}
public void rotate(){
int tmp = w;
w = h;
h = tmp;
}
public Rectangle(int h, int w){
this.h = h;
this.w = w;
}
public Rectangle(){};
}
private static Rectangle[] rects = new Rectangle[4];
private static int minArea = Integer.MAX_VALUE;
private static ArrayList<Rectangle> ar = new ArrayList<Rectangle>();
public static void swap(int i, int j){
assert(i >= 0 && i < 4);
assert(j >= 0 && j < 4);
Rectangle tmp = rects[i];
rects[i] = rects[j];
rects[j] = tmp;
}
public static int max(int ... arg){
int max = 0;
for(int a: arg){
if(a > max)
max = a;
}
return max;
}
public static void mayAdd(int h, int w){
int area = h * w;
if(area > minArea)
return ;
Rectangle r =new Rectangle(h, w).makeStandard();
if(area == minArea ){
if(!ar.contains(r))
ar.add(r);
return ;
}
ar.clear();
minArea = area;
ar.add(r);
}
public static void calculate6Case(){
Rectangle r = new Rectangle();
int h, w;
//case one
{
h = max(rects[0].h,rects[1].h,rects[2].h,rects[3].h);
w = rects[0].w+rects[1].w+rects[2].w+rects[3].w;
mayAdd(h, w);
}
//case two
{
h = rects[0].w + max(rects[1].h,rects[2].h,rects[3].h);
w = max(rects[0]. h, rects[1].w+rects[2].w+rects[3].w);
mayAdd(h, w);
}
//case three
{
h = max(rects[0].w + rects[1].h, rects[0].w + rects[2].h, rects[3].h);
w = max(rects[3].w + rects[0].h, rects[1].w+rects[2].w+rects[3].w);
mayAdd(h, w);
}
//case four five
{
h = max(rects[0].h,rects[1].h +rects[2].h,rects[3].h);
w = max(rects[0].w+rects[2].w+rects[3].w, rects[0].w+rects[1].w+rects[3].w);
mayAdd(h, w);
}
//case six
{
if(rects[0].w <= rects[3].h){
h = max(rects[0].w+rects[1].h,rects[2].w+rects[3].h);
if(rects[3].h >=rects[0].w+rects[1].h ){
w = max(max(rects[1].w, rects[0].h) + rects[3].w, rects[2].h);
}else if(rects[3].w >= rects[2].h){
w = max(rects[1].w, rects[0].h) + rects[3].w;
}else {
w = max(rects[1].w + rects[2].h, rects[0].h + rects[3].w);
}
mayAdd(h, w);
}
}
}
public static void solve(int n){
if(n == 4){
calculate6Case();
return ;
}
solve(n+1);
rects[n].rotate();
solve(n+1);
rects[n].rotate();
}
public static void permutation(int start){
if(start >= 4){
//solve(0);
solve(0);
}
for(int i = start; i < rects.length; i++){
swap(start, i);
permutation(start+1);
swap(start, i);
}
}
public static void main(String[]args) throws IOException{
BufferedReader f = new BufferedReader(new FileReader("packrec.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("packrec.out")));
for(int i = 0; i < 4; i++){
StringTokenizer st = new StringTokenizer(f.readLine());
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
rects[i] = new Rectangle(h, w);
}
permutation(0);
// for(Rectangle r : ar){
// r.makeStandard();
// }
Collections.sort(ar);
out.println(minArea);
for(Rectangle r : ar){
out.println(r.w+" "+r.h);
}
out.close();
}
}