Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2022 /finals /cup_counterbalancing.java
wjomlex's picture
2022 Problems
f7ba5f2 verified
raw
history blame
12.3 kB
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class CupCounterbalancing {
static PrintWriter out;
static void solve(Scanner fs, PrintWriter out, int tt) throws InterruptedException {
int n = fs.nextInt(), r = fs.nextInt(), max = fs.nextInt();
Vec[] points = new Vec[n];
for (int i = 0; i < n; i++) {
points[i] = new Vec(fs.nextInt(), fs.nextInt());
}
double ans = solveNaive1e8(points, r, max);
out.println("Case #" + tt + ": " + ans);
out.flush();
}
static double solveNaive1e8(Vec[] points, int r, int max) {
long time = System.currentTimeMillis();
Vec test = new Vec(1, 0).rotate(random.nextDouble()).scale(max / Math.sqrt(1e8));
double res = solveNaive(points, r, max, test);
return res;
}
static double solveNaive1e8Multithreaded(
Vec[] points, int r, int max, boolean print, int nThreads) throws InterruptedException {
long time = System.currentTimeMillis();
Thread[] threads = new Thread[nThreads];
final double[] answers = new double[nThreads];
for (int i = 0; i < threads.length; i++) {
final int ii = i;
threads[i] = new Thread(null, null, "T") {
public void run() {
answers[ii] = solveNaive1e8(points, r, max);
}
};
threads[i].start();
}
for (Thread t : threads) {
t.join();
}
double average = 0;
for (double d : answers) {
average += d;
}
return average / nThreads;
}
static Random random = new Random();
static double solveNaive(Vec[] points, int r, int max, Vec test) {
test.x = Math.abs(test.x);
test.y = Math.abs(test.y);
Vec start = new Vec(0, 0);
int row = 0;
Vec rotated = test.rot90();
Seg[] segs = new Seg[points.length];
for (int i = 0; i < points.length; i++) {
segs[i] = new Seg(points[i], points[(i + 1) % points.length]);
}
int worksCount = 0;
int allCount = 0;
while (true) {
Vec rowStart = start.add(rotated.scale(row));
if (rowStart.y > max)
break;
Vec at = rowStart;
while (at.x <= max && at.y <= max) {
if (at.x >= 0 && at.y >= 0) {
if (worksNaive(segs, at, r)) {
worksCount++;
}
allCount++;
}
at = at.add(test);
}
row++;
}
row = -1;
while (true) {
Vec rowStart = start.add(rotated.scale(row));
Vec at = rowStart;
boolean hit = false;
while (at.x <= max && at.y <= max) {
if (at.x >= 0 && at.y >= 0) {
hit = true;
if (worksNaive(segs, at, r)) {
worksCount++;
}
allCount++;
}
at = at.add(test);
}
row--;
if (!hit) {
break;
}
}
return worksCount / (double) allCount;
}
static boolean worksNaive(Seg[] segs, Vec query, long r) {
ArrayList<Vec> intersections = new ArrayList<>();
Circle c = new Circle(query, r);
for (Seg s : segs) {
Vec[] ans = c.intersectSeg(s);
if (ans != null) {
for (Vec v : ans) {
intersections.add(v);
}
}
}
double[] angles = new double[intersections.size()];
for (int i = 0; i < angles.length; i++) angles[i] = intersections.get(i).sub(query).angle();
Arrays.sort(angles);
if (angles.length <= 2) {
return false;
}
for (int i = 1; i < angles.length; i++) {
if (angles[i] - angles[i - 1] >= Math.PI) {
return false;
}
}
if (angles[0] - (angles[angles.length - 1] - Math.PI * 2) > Math.PI) {
return false;
}
return true;
}
static class Vec {
static final double EPS = 1e-6;
double x, y;
public Vec(double x, double y) {
this.x = x;
this.y = y;
}
public Vec add(Vec o) { return new Vec(x + o.x, y + o.y); }
public Vec sub(Vec o) { return new Vec(x - o.x, y - o.y); }
public Vec scale(double s) { return new Vec(x * s, y * s); }
public double dot(Vec o) { return x * o.x + y * o.y; }
public double cross(Vec o) { return x * o.y - y * o.x; }
public double mag2() { return dot(this); }
public double mag() { return Math.sqrt(mag2()); }
public Vec unit() { return scale(1 / mag()); }
public Vec rot90() { return new Vec(-y, x); }
public Vec rot270() { return new Vec(y, -x); }
public Vec rotate(double theta) {
double PI = Math.PI;
double newX = x * Math.cos(theta) + y * Math.cos(PI / 2 + theta);
double newY = x * Math.sin(theta) + y * Math.sin(PI / 2 + theta);
return new Vec(newX, newY);
}
// Angle between 0 and 2PI
public double angle() {
return (Math.atan2(y, x) + 2 * Math.PI) % (2 * Math.PI);
}
public String toString() {
DecimalFormat df = new DecimalFormat("#.##");
return "(" + df.format(x) + ", " + df.format(y) + ")";
}
static boolean eq(double a, double b) {
return Math.abs(a - b) < EPS;
}
static boolean leq(double a, double b) {
return a - EPS < b;
}
static boolean geq(double a, double b) {
return a + EPS > b;
}
public boolean equals(Object oo) {
Vec o = (Vec) oo;
return eq(x, o.x) && eq(y, o.y);
}
}
static class Seg {
Vec from, to, dir;
public Seg(Vec from, Vec to) {
this.from = from;
this.to = to;
dir = to.sub(from);
}
// Line-line intersection
public Vec lineIntersect(Seg o) {
double det = o.dir.x * dir.y - dir.x * o.dir.y;
if (Vec.eq(det, 0))
return null;
double dist = (o.dir.x * (o.from.y - from.y) - o.dir.y * (o.from.x - from.x)) / det;
return from.add(dir.scale(dist));
}
public boolean containsPoint(Vec o) {
double distFromLine = dir.unit().cross(o.sub(from));
if (!Vec.eq(distFromLine, 0)) {
return false;
}
return Vec.eq(dir.mag(), from.sub(o).mag() + to.sub(o).mag());
}
// Seg-seg intersection
public Vec segIntersection(Seg o) {
Vec intersect = lineIntersect(o);
if (intersect == null) {
return null;
}
return containsPoint(intersect) && o.containsPoint(intersect) ? intersect : null;
}
// Returns 1 if above, 0 if on, -1 if below.
public int side(Vec o) {
Vec oDir = o.sub(from);
double distFromLine = dir.unit().cross(oDir);
return Vec.eq(distFromLine, 0) ? 0 : (int)Math.signum(distFromLine);
}
public boolean intersects(Seg o) {
return side(o.from) != side(o.to) && o.side(from) != o.side(to);
}
public Vec getClosestTo(Vec o) {
double percentThere = o.sub(from).dot(dir) / dir.mag2();
return from.add(dir.scale(Math.max(0, Math.min(1, percentThere))));
}
public Vec projectToLine(Vec o) {
return dir.scale(o.sub(from).dot(dir) / dir.mag2()).add(from);
}
// Returns the shortest segment from the x-axis to the point to the y axis.
// toContain must be a point with positive x and y coordinates.
public static Seg getShortestSegFromAxesContainingQ1Point(Vec toContain) {
double slope = -Math.pow(toContain.y / toContain.x, 1.0 / 3);
double b = toContain.y - toContain.x * slope;
double xInt = -b / slope;
return new Seg(new Vec(0, b), new Vec(xInt, 0));
}
public String toString() {
return from + " -> " + to;
}
}
static class Circle {
Vec c;
double r;
public Circle(Vec c, double r) {
this.c = c;
this.r = r;
}
public boolean contains(Vec v) {
return c.sub(v).mag2() - Vec.EPS * Vec.EPS <= r * r;
}
// When standing at this circle, returns right tangent, then left tangent.
public Vec[] intersect(Circle o) {
if (c.equals(o.c)) {
return null;
}
Vec dir = o.c.sub(c);
double d2 = dir.mag2(), d = Math.sqrt(d2);
if (r + o.r < d || r + d < o.r || o.r + d < r) {
return null;
}
if (Vec.eq(r + o.r, d) || Vec.eq(o.r + d, r)) {
return new Vec[] {c.add(dir.scale(r / d))};
}
if (Vec.eq(r + d, o.r)) {
return new Vec[] {c.sub(dir.scale(r / d))};
}
double d1 = (r * r + d2 - o.r * o.r) / (2 * d);
double h = Math.sqrt(r * r - d1 * d1);
Vec unitDir = dir.unit();
Vec rInt = c.add(unitDir.scale(d1).add(unitDir.rot270().scale(h)));
Vec lInt = c.add(unitDir.scale(d1).add(unitDir.rot90().scale(h)));
return new Vec[] {rInt, lInt};
}
public double intersectionArea(Circle o) {
double d = o.c.sub(c).mag();
if (r + o.r < d) {
return 0;
}
double minR = Math.min(r, o.r), maxR = Math.max(r, o.r), pi = Math.PI;
if (Vec.leq(d + minR, maxR)) {
return pi * minR * minR;
}
double theta1 = 2 * Math.acos((r * r + d * d - o.r * o.r) / (2 * r * d));
double theta2 = 2 * Math.acos((o.r * o.r + d * d - r * r) / (2 * o.r * d));
double part1Area = theta1 / 2 * r * r;
double part2Area = theta2 / 2 * o.r * o.r;
double tri1 = r * r * Math.sin(theta1) / 2;
double tri2 = o.r * o.r * Math.sin(theta2) / 2;
return part1Area + part2Area - tri1 - tri2;
}
// Returns right tangent, then left tangent from perspective of the point
public Vec[] getTangentPoints(Vec p) {
if (contains(p)) {
return null;
}
double d2 = c.sub(p).mag2();
return new Circle(p, Math.sqrt(d2 - r * r)).intersect(this);
}
// Line going from my left to his right, then my right to his left lines go
// from me to him
public Seg[] internalTangentLines(Circle o) {
Vec[] tangentPoints = new Circle(c, r + o.r).getTangentPoints(o.c);
Vec offset1 = tangentPoints[0].sub(o.c).rot90().unit().scale(o.r);
Vec offset2 = tangentPoints[1].sub(o.c).rot270().unit().scale(o.r);
return new Seg[] {
new Seg(tangentPoints[0].add(offset1), o.c.add(offset1)),
new Seg(tangentPoints[1].add(offset2), o.c.add(offset2))
};
}
// Right external tangent, then left external tangent, from my perspective
// lines go from me to him
public Seg[] externalTangentLines(Circle o) {
if (o.r > r) {
Seg[] oAnswer = o.externalTangentLines(this);
return new Seg[] {
new Seg(oAnswer[1].to, oAnswer[1].from),
new Seg(oAnswer[0].to, oAnswer[0].from)
};
}
Vec[] tangentPoints = new Circle(c, r - o.r).getTangentPoints(o.c);
Vec offset1 = tangentPoints[0].sub(o.c).rot270().unit().scale(o.r);
Vec offset2 = tangentPoints[1].sub(o.c).rot90().unit().scale(o.r);
return new Seg[] {
new Seg(tangentPoints[1].add(offset2), o.c.add(offset2)),
new Seg(tangentPoints[0].add(offset1), o.c.add(offset1))
};
}
// Line (not line segment)-circle intersection in the order of line.dir
public Vec[] intersectLine(Seg line) {
Vec closest = line.projectToLine(c);
double d2 = closest.sub(c).mag2();
if (d2 > r * r) {
return null;
}
double l = Math.sqrt(r * r - d2);
if (Vec.eq(l, 0)) {
return new Vec[] {closest};
}
Vec lVec = line.dir.unit().scale(l);
return new Vec[] {closest.sub(lVec), closest.add(lVec)};
}
// Line segment-circle intersection
public Vec[] intersectSeg(Seg seg) {
Vec[] lineIntersections = intersectLine(seg);
if (lineIntersections == null)
return null;
ArrayList<Vec> contained = new ArrayList<>();
for (Vec v : lineIntersections) {
if (seg.containsPoint(v)) {
contained.add(v);
}
}
if (contained.isEmpty()) {
return null;
}
return contained.toArray(new Vec[contained.size()]);
}
public String toString() {
DecimalFormat df = new DecimalFormat();
return "center: " + c + ", r: " + df.format(r);
}
}
public static void main(String[] args) throws InterruptedException {
Scanner fs = new Scanner(System.in);
out = new PrintWriter(System.out);
int T = fs.nextInt();
for (int t = 1; t <= T; t++) {
solve(fs, out, t);
}
fs.close();
out.close();
}
}