11 public BufferedReader f
;
13 public PrintWriter out
;
17 public IO () throws IOException
{
18 f
= new BufferedReader(new FileReader(name
+ ".in"));
19 out
= new PrintWriter(new BufferedWriter(new FileWriter(name
+ ".out")));
22 public String
nextLine() throws IOException
{
28 public int nextIntLine() throws IOException
{
29 return Integer
.parseInt(nextLine());
32 public void tokenize() {
33 st
= new StringTokenizer(line
);
36 public String
nextToken() {
37 if(st
== null) tokenize();
38 return st
.nextToken();
41 public int nextIntToken() {
42 return Integer
.parseInt(nextToken());
46 out
.close(); // close the output file
47 System
.exit(0); // don't omit this!
51 static boolean debug
= false;
52 static String name
= "packrec";
56 //////////////////////////////////////////
59 static TreeMap
<Integer
, Integer
> min_map
;
64 public Rect (int x
, int y
) {
69 Rect
growRight(Rect peer
){
70 return new Rect(this.x
+ peer
.x
, this.y
> peer
.y ?
this.y
: peer
.y
);
73 Rect
growDown(Rect peer
) {
74 return new Rect(this.x
> peer
.x ?
this.x
: peer
.x
, this.y
+ peer
.y
);
78 return x
* y
< min_area
; // search tree pruning
89 if(min_area
== area
) {
90 if(x
<= y
) min_map
.put(x
, y
);
91 else min_map
.put(y
, x
);
96 static void checkLast(Rect r1
, Rect r2
, Rect r3
, Rect r4
){
105 if(mx
< r2
.x
) mx
= r2
.x
;
110 if(r2
.y
>= r3
.y
+ r4
.y
){
111 return; // this is equivalent to layout 3
118 if(r1
.y
+ r2
.y
<= r3
.y
){
119 return; // this is equivalent to layout 3
129 int my
= r1
.y
+ r2
.y
;
133 (new Rect(mx
, my
)).checkResult();
136 static void checkOther(Rect r1
, Rect r2
, Rect r3
, Rect r4
){
137 final Rect block
= r1
.growRight(r2
);
140 block
.growDown(r3
).growRight(r4
).checkResult(); // layout 3
141 block
.growRight(r3
.growDown(r4
)).checkResult(); // layout 5
144 checkLast(r1
, r2
, r3
, r4
);
145 checkLast(r2
, r1
, r3
, r4
);
148 static void iterate3rd(Rect r1
, Rect r2
, Rect r3
, Rect r4
){
149 final Rect block
= r1
.growRight(r2
).growRight(r3
);
152 block
.growRight(r4
).checkResult(); // layout 1
153 block
.growDown(r4
).checkResult(); // layout 2
156 // iterate 3rd parameter
157 checkOther(r1
, r2
, r3
, r4
);
158 checkOther(r1
, r3
, r2
, r4
);
159 checkOther(r2
, r3
, r1
, r4
);
162 static void iterate4th(
163 int x1
, int y1
, int x2
, int y2
,
164 int x3
, int y3
, int x4
, int y4
)
166 Rect r1
= new Rect(x1
, y1
);
167 Rect r2
= new Rect(x2
, y2
);
168 Rect r3
= new Rect(x3
, y3
);
169 Rect r4
= new Rect(x4
, y4
);
171 // iterate 4th parameter
172 iterate3rd(r1
, r2
, r3
, r4
);
173 iterate3rd(r1
, r2
, r4
, r3
); // NOTE: checkOther performs the same blockcheck twice, but we dont care for now
174 iterate3rd(r1
, r3
, r4
, r2
);
175 iterate3rd(r2
, r3
, r4
, r1
);
178 public static void main (String
[] args
) throws IOException
{
182 final int x1
= io
.nextIntToken();
183 final int y1
= io
.nextIntToken();
186 final int x2
= io
.nextIntToken();
187 final int y2
= io
.nextIntToken();
190 final int x3
= io
.nextIntToken();
191 final int y3
= io
.nextIntToken();
194 final int x4
= io
.nextIntToken();
195 final int y4
= io
.nextIntToken();
197 min_area
= (x1
+ x2
+ x3
+ x4
) * (y1
+ y2
+ y3
+ y4
) * 2;
199 min_map
= new TreeMap
<Integer
, Integer
> ();
201 // iterate all transpositions
202 iterate4th(x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
);
203 iterate4th(x1
, y1
, x2
, y2
, x3
, y3
, y4
, x4
);
205 iterate4th(x1
, y1
, x2
, y2
, y3
, x3
, x4
, y4
);
206 iterate4th(x1
, y1
, x2
, y2
, y3
, x3
, y4
, x4
);
209 iterate4th(x1
, y1
, y2
, x2
, x3
, y3
, x4
, y4
);
210 iterate4th(x1
, y1
, y2
, x2
, x3
, y3
, y4
, x4
);
212 iterate4th(x1
, y1
, y2
, x2
, y3
, x3
, x4
, y4
);
213 iterate4th(x1
, y1
, y2
, x2
, y3
, x3
, y4
, x4
);
216 iterate4th(y1
, x1
, x2
, y2
, x3
, y3
, x4
, y4
);
217 iterate4th(y1
, x1
, x2
, y2
, x3
, y3
, y4
, x4
);
219 iterate4th(y1
, x1
, x2
, y2
, y3
, x3
, x4
, y4
);
220 iterate4th(y1
, x1
, x2
, y2
, y3
, x3
, y4
, x4
);
223 iterate4th(y1
, x1
, y2
, x2
, x3
, y3
, x4
, y4
);
224 iterate4th(y1
, x1
, y2
, x2
, x3
, y3
, y4
, x4
);
226 iterate4th(y1
, x1
, y2
, x2
, y3
, x3
, x4
, y4
);
227 iterate4th(y1
, x1
, y2
, x2
, y3
, x3
, y4
, x4
);
230 io
.out
.println(min_area
);
232 Map
.Entry
<Integer
, Integer
> entry
;
234 while((entry
= min_map
.pollFirstEntry()) != null) {
235 io
.out
.printf("%d %d\n", entry
.getKey(), entry
.getValue());