Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 12,295 Bytes
f7ba5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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();
  }
}