-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplittingthefield.java
More file actions
146 lines (131 loc) · 3.46 KB
/
splittingthefield.java
File metadata and controls
146 lines (131 loc) · 3.46 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
/*
ID: grifync1
LANG: JAVA
PROG: splittingthefield
*/
//:O :/ :) :D :(
//lil lil peezy
import java.util.*;
import java.io.*;
public class splittingthefield {
public static int conv(String s) {
return Integer.parseInt(s);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static void print(int num) {
System.out.println(num);
}
public static void prints(String s) {
System.out.println(s);
}
public static void printa(int[] a) {
for (int i = 0; i < a.length; i++) {
if (i == 0) {
System.out.print(a[i]);
} else {
System.out.print(" " + a[i]);
}
}
}
public static int[] sort(int[] nums) {
Arrays.sort(nums);
return nums;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("split.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("split.out")));
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//PrintWriter out = new PrintWriter(System.out);
//StringTokenizer st = new StringTokenizer(in.readLine());
int len = conv(in.readLine());
Point[] points = new Point[len];
int minx = Integer.MAX_VALUE;
int maxx = -1;
int miny = Integer.MAX_VALUE;
int maxy = -1;
for(int i=0; i<len; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int a = conv(st.nextToken());
int b = conv(st.nextToken());
minx = min(minx, a);
maxx= max(maxx, a);
miny= min(miny, b);
maxy = max(maxy, b);
points[i] = new Point(a, b);
}
long prt = horiz(points, len, maxx, minx, maxy, miny);
for(int i=0; i<len; i++) {
int tmp = points[i].y;
points[i].y=points[i].x;
points[i].x=tmp;
}
long pt = horiz(points, len, maxx, minx, maxy, miny);
out.println(Math.max(pt, prt));
in.close();
out.close();
}
static class Point implements Comparable<Point>{
int x,y;
public Point(int a, int b) {
this.x=a;
this.y=b;
}
@Override
public int compareTo(Point o) {
return this.x-o.x;
}
}
public static long horiz(Point[] points, int len, int maxx, int minx, int maxy, int miny) {
int[][] mim = new int[len][2];
int[][] mam = new int[len][2];
long ina = ((long)((maxx-minx))*(long)((maxy-miny)));
Arrays.sort(points);
mim[0][0]=points[0].y;
mim[len-1][1]=points[len-1].y;
mam[0][0]=points[0].y;
mam[len-1][1]=points[len-1].y;
for(int i=1; i<len; i++) {
mim[i][0] = min(mim[i-1][0], points[i].y);
mam[i][0] = max(mam[i-1][0], points[i].y);
}
for(int i=len-2; i>=0; i--) {
mim[i][1]=min(mim[i+1][1], points[i].y);
mam[i][1]=max(mam[i+1][1], points[i].y);
}
Arrays.sort(points);
mim[0][0]=points[0].y;
mim[len-1][1]=points[len-1].y;
mam[0][0]=points[0].y;
mam[len-1][1]=points[len-1].y;
for(int i=1; i<len; i++) {
mim[i][0] = min(mim[i-1][0], points[i].y);
mam[i][0] = max(mam[i-1][0], points[i].y);
}
for(int i=len-2; i>=0; i--) {
mim[i][1]=min(mim[i+1][1], points[i].y);
mam[i][1]=max(mam[i+1][1], points[i].y);
}
long ans1 = Long.MAX_VALUE;
for(int j=0; j<len-1; j++) {
int r = points[j].x;
int l2 = points[j+1].x;
if(r==l2) {
continue;
}
int l = points[0].x;
int r2 = points[len-1].x;
long xdifl = r-l;
long xdifr = r2-l2;
long ydifl = mam[j][0]-mim[j][0];
long ydifr = mam[j+1][1]-mim[j+1][1];
long sarea = xdifl*ydifl+(xdifr*ydifr);
ans1 = Math.min(ans1, sarea);
}
return ina-ans1;
}
}