Last active
December 11, 2015 01:28
-
-
Save mastersobg/4523378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import static java.lang.Math.*; | |
public class Solution { | |
BufferedReader in; | |
StringTokenizer st; | |
PrintWriter out; | |
String filename = "rag"; | |
static class Point { | |
double x; | |
int y; | |
public Point(double x, int y) { | |
super(); | |
this.x = x; | |
this.y = y; | |
} | |
} | |
int X, Y; | |
int n; | |
Point[] v; | |
void solve() throws IOException { | |
X = ni(); | |
Y = ni(); | |
n = ni(); | |
v = new Point[n]; | |
for (int i = 0; i < n; ++i) { | |
int x = ni(); | |
int y = ni(); | |
v[i] = new Point(x, y); | |
} | |
Point[] stack = new Point[n + 1]; | |
int sz = 0; | |
stack[sz++] = v[0]; | |
stack[sz++] = v[1]; | |
for (int i = 2; i < n; ++i) { | |
if (stack[sz - 1].y < v[i].y && v[i].x > stack[sz - 1].x) { | |
while (sz > 0 && stack[sz - 1].y < v[i].y | |
&& v[i].x > stack[sz - 1].x) | |
--sz; | |
double ty = stack[sz].y - stack[sz - 1].y; | |
double tx = stack[sz].x - stack[sz - 1].x; | |
double dx = (v[i].y - stack[sz - 1].y) * tx / ty; | |
stack[sz] = new Point(stack[sz - 1].x + dx, v[i].y); | |
sz++; | |
} | |
stack[sz++] = v[i]; | |
} | |
// reverse | |
v = stack.clone(); | |
sz = 0; | |
stack[sz++] = v[n - 1]; | |
stack[sz++] = v[n - 2]; | |
for (int i = n - 3; i >= 0; --i) { | |
if (stack[sz - 1].y > v[i].y && v[i].x > stack[sz - 1].x) { | |
while (sz > 0 && stack[sz - 1].y > v[i].y | |
&& v[i].x > stack[sz - 1].x) | |
--sz; | |
double ty = stack[sz].y - stack[sz - 1].y; | |
double tx = stack[sz].x - stack[sz - 1].x; | |
double dx = (v[i].y - stack[sz - 1].y) * tx / ty; | |
stack[sz] = new Point(stack[sz - 1].x + dx, v[i].y); | |
sz++; | |
} | |
stack[sz++] = v[i]; | |
} | |
stack[sz++] = new Point(X, Y); | |
double ret = 0.0; | |
for (int i = 0; i < sz; ++i) { | |
int j = (i + 1) % sz; | |
Point p1 = stack[i], p2 = stack[j]; | |
ret += (p1.y + p2.y) * (p2.x - p1.x); | |
} | |
ret *= 0.5; | |
out.println(abs(ret)); | |
} | |
public Solution() throws IOException { | |
Locale.setDefault(Locale.US); | |
in = new BufferedReader(new FileReader(filename + ".in")); | |
out = new PrintWriter(filename + ".out"); | |
solve(); | |
in.close(); | |
out.close(); | |
} | |
String ns() throws IOException { | |
while (st == null || !st.hasMoreTokens()) | |
st = new StringTokenizer(in.readLine()); | |
return st.nextToken(); | |
} | |
int ni() throws IOException { | |
return Integer.valueOf(ns()); | |
} | |
long nl() throws IOException { | |
return Long.valueOf(ns()); | |
} | |
double nd() throws IOException { | |
return Double.valueOf(ns()); | |
} | |
public static void main(String[] args) throws IOException { | |
new Solution(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment