結果

問題 No.2602 Real Collider
ユーザー Maksym ShcherbanMaksym Shcherban
提出日時 2024-01-12 23:28:18
言語 JavaScript
(node v21.7.1)
結果
WA  
実行時間 -
コード長 4,921 bytes
コンパイル時間 68 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 69,468 KB
最終ジャッジ日時 2024-09-28 00:15:43
合計ジャッジ時間 62,550 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}
class Circle {
    constructor(x, y, r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }
}
/*
 * Returns the smallest circle that encloses all the given points. Runs in expected O(n) time, randomized.
 * Note: If 0 points are given, null is returned. If 1 point is given, a circle of radius 0 is returned.
 */
// Initially: No boundary points known
function makeCircle(points) {
    // Clone list to preserve the caller's data, do Durstenfeld shuffle
    let shuffled = points.slice();
    for (let i = points.length - 1; i >= 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        j = Math.max(Math.min(j, i), 0);
        const temp = shuffled[i];
        shuffled[i] = shuffled[j];
        shuffled[j] = temp;
    }
    // Progressively add points to circle or recompute circle
    let c = null;
    shuffled.forEach((p, i) => {
        if (c === null || !isInCircle(c, p))
            c = makeCircleOnePoint(shuffled.slice(0, i + 1), p);
    });
    return c;
}
// One boundary point known
function makeCircleOnePoint(points, p) {
    let c = new Circle(p.x, p.y, 0);
    points.forEach((q, i) => {
        if (!isInCircle(c, q)) {
            if (c.r == 0)
                c = makeDiameter(p, q);
            else
                c = makeCircleTwoPoints(points.slice(0, i + 1), p, q);
        }
    });
    return c;
}
// Two boundary points known
function makeCircleTwoPoints(points, p, q) {
    const circ = makeDiameter(p, q);
    let left = null;
    let right = null;
    // For each point not in the two-point circle
    for (const r of points) {
        if (isInCircle(circ, r))
            continue;
        // Form a circumcircle and classify it on left or right side
        const cross = crossProduct(p.x, p.y, q.x, q.y, r.x, r.y);
        const c = makeCircumcircle(p, q, r);
        if (c === null)
            continue;
        else if (cross > 0 && (left === null || crossProduct(p.x, p.y, q.x, q.y, c.x, c.y) > crossProduct(p.x, p.y, q.x, q.y, left.x, left.y)))
            left = c;
        else if (cross < 0 && (right === null || crossProduct(p.x, p.y, q.x, q.y, c.x, c.y) < crossProduct(p.x, p.y, q.x, q.y, right.x, right.y)))
            right = c;
    }
    // Select which circle to return
    if (left === null && right === null)
        return circ;
    else if (left === null && right !== null)
        return right;
    else if (left !== null && right === null)
        return left;
    else if (left !== null && right !== null)
        return left.r <= right.r ? left : right;
    else
        throw new Error("Assertion error");
}
function makeDiameter(a, b) {
    const cx = (a.x + b.x) / 2;
    const cy = (a.y + b.y) / 2;
    const r0 = distance(cx, cy, a.x, a.y);
    const r1 = distance(cx, cy, b.x, b.y);
    return new Circle(cx, cy, Math.max(r0, r1));
}
function makeCircumcircle(a, b, c) {
    // Mathematical algorithm from Wikipedia: Circumscribed circle
    const ox = (Math.min(a.x, b.x, c.x) + Math.max(a.x, b.x, c.x)) / 2;
    const oy = (Math.min(a.y, b.y, c.y) + Math.max(a.y, b.y, c.y)) / 2;
    const ax = a.x - ox;
    const ay = a.y - oy;
    const bx = b.x - ox;
    const by = b.y - oy;
    const cx = c.x - ox;
    const cy = c.y - oy;
    const d = (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by)) * 2;
    if (d == 0)
        return null;
    const x = ox + ((ax * ax + ay * ay) * (by - cy) + (bx * bx + by * by) * (cy - ay) + (cx * cx + cy * cy) * (ay - by)) / d;
    const y = oy + ((ax * ax + ay * ay) * (cx - bx) + (bx * bx + by * by) * (ax - cx) + (cx * cx + cy * cy) * (bx - ax)) / d;
    const ra = distance(x, y, a.x, a.y);
    const rb = distance(x, y, b.x, b.y);
    const rc = distance(x, y, c.x, c.y);
    return new Circle(x, y, Math.max(ra, rb, rc));
}
/* Simple mathematical functions */
const MULTIPLICATIVE_EPSILON = 1 + 1e-14;
function isInCircle(c, p) {
    return c !== null && distance(p.x, p.y, c.x, c.y) <= c.r * MULTIPLICATIVE_EPSILON;
}
// Returns twice the signed area of the triangle defined by (x0, y0), (x1, y1), (x2, y2).
function crossProduct(x0, y0, x1, y1, x2, y2) {
    return (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
}
function distance(x0, y0, x1, y1) {
    return Math.hypot(x0 - x1, y0 - y1);
}

function Main(input) {
  const lines = input.split('\n').map(x => x.trim()).filter(Boolean);
  const q = +lines[0];
  const [xa, ya, xb, yb, xc, yc] = lines[1].split(' ').map(Number);
  const circle = makeCircle([
    new Point(xa, ya),
    new Point(xb, yb),
    new Point(xc, yc),
  ]);
  console.log(circle);
  for (let i = 0; i < q; i++) {
    const [x, y] = lines[2 + i].split(' ').map(Number);
    const d = Math.hypot(x - circle.x, y - circle.y);
    const result = d <= (circle.r + 1e-7) ? 'Yes' : 'No';
    console.log(result);
  }
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
0