結果

問題 No.202 1円玉投げ
ユーザー KilisameKilisame
提出日時 2016-06-10 16:13:43
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,008 ms / 5,000 ms
コード長 6,709 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 75,388 KB
実行使用メモリ 71,524 KB
最終ジャッジ日時 2023-08-23 23:24:06
合計ジャッジ時間 25,703 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 960 ms
71,436 KB
testcase_01 AC 966 ms
71,524 KB
testcase_02 AC 122 ms
55,536 KB
testcase_03 AC 122 ms
56,152 KB
testcase_04 AC 122 ms
56,260 KB
testcase_05 AC 292 ms
61,804 KB
testcase_06 AC 862 ms
71,020 KB
testcase_07 AC 896 ms
70,892 KB
testcase_08 AC 895 ms
70,764 KB
testcase_09 AC 666 ms
65,924 KB
testcase_10 AC 428 ms
63,204 KB
testcase_11 AC 578 ms
65,420 KB
testcase_12 AC 588 ms
65,684 KB
testcase_13 AC 445 ms
63,344 KB
testcase_14 AC 310 ms
61,136 KB
testcase_15 AC 657 ms
65,776 KB
testcase_16 AC 875 ms
70,664 KB
testcase_17 AC 898 ms
70,644 KB
testcase_18 AC 902 ms
70,948 KB
testcase_19 AC 638 ms
65,548 KB
testcase_20 AC 860 ms
69,056 KB
testcase_21 AC 646 ms
65,576 KB
testcase_22 AC 213 ms
60,192 KB
testcase_23 AC 209 ms
60,092 KB
testcase_24 AC 211 ms
59,976 KB
testcase_25 AC 211 ms
60,204 KB
testcase_26 AC 203 ms
59,820 KB
testcase_27 AC 210 ms
59,420 KB
testcase_28 AC 210 ms
60,236 KB
testcase_29 AC 199 ms
59,580 KB
testcase_30 AC 210 ms
59,736 KB
testcase_31 AC 208 ms
60,192 KB
testcase_32 AC 209 ms
59,944 KB
testcase_33 AC 212 ms
57,216 KB
testcase_34 AC 216 ms
60,300 KB
testcase_35 AC 992 ms
71,056 KB
testcase_36 AC 1,007 ms
71,196 KB
testcase_37 AC 914 ms
70,880 KB
testcase_38 AC 1,008 ms
71,496 KB
testcase_39 AC 180 ms
55,712 KB
testcase_40 AC 170 ms
56,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

    private static final int BLOCK_SIZE = 200;

    private static Set<Point>[][] leftOfCoinSet = new Set[20000 / BLOCK_SIZE + 1][20000 / BLOCK_SIZE + 1];

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int limit = cin.nextInt();
        int leftCount = 0;
        for (int i = 0; i < limit; i++) {
            int x = cin.nextInt();
            int y = cin.nextInt();
            /* x,yそれぞれ1/200して、200*200の範囲内で干渉しあう円の中心情報を得る */
            if (isLeft(x, y)) {
                leftCount++;
            }
        }
        System.out.println(leftCount);
    }

    private static boolean isLeft(int x, int y) {
        Point thisCoin = new Point(x, y);
        /* 落ちたコインの中心座標からブロックを求める */
        int blockX = x / BLOCK_SIZE;
        int blockY = y / BLOCK_SIZE;
        /* どのブロックにまたがっているか。順に左、右、下、上、左下、右下、左上、右上で8要素配列 */
        boolean[] isStradding = new boolean[4];
        List<Set<Point>> checkPointList = new ArrayList<Set<Point>>();
        if (leftOfCoinSet[blockX][blockY] == null) {
            leftOfCoinSet[blockX][blockY] = new HashSet<Point>();
        }
        checkPointList.add(leftOfCoinSet[blockX][blockY]);
        if (blockX > 0 && x % BLOCK_SIZE < 10) {
            /* 左チェック。左端以外のBlockでかつ、x座標をBLOCK_SIZEで割った時の余りが半径の10に満たない場合 */
            isStradding[0] = true;
            if (leftOfCoinSet[blockX - 1][blockY] == null) {
                leftOfCoinSet[blockX - 1][blockY] = new HashSet<Point>();
            }
            checkPointList.add(leftOfCoinSet[blockX - 1][blockY]);
        } else if (blockX < 20000 / BLOCK_SIZE && x % BLOCK_SIZE >= BLOCK_SIZE - 10) {
            /* 右チェック。右端以外のBlockでかつ、x座標をBLOCK_SIZEで割った時の余りがBLOCK_SIZEから半径の10を引いた値以上になる場合 */
            isStradding[1] = true;
            if (leftOfCoinSet[blockX + 1][blockY] == null) {
                leftOfCoinSet[blockX + 1][blockY] = new HashSet<Point>();
            }
            checkPointList.add(leftOfCoinSet[blockX + 1][blockY]);
        }
        if (blockY > 0 && y % BLOCK_SIZE < 10) {
            /* 下チェック。下端以外のBlockでかつ、y座標をBLOCK_SIZEで割った時の余りが半径の10に満たない場合 */
            isStradding[2] = true;
            if (leftOfCoinSet[blockX][blockY - 1] == null) {
                leftOfCoinSet[blockX][blockY - 1] = new HashSet<Point>();
            }
            checkPointList.add(leftOfCoinSet[blockX][blockY - 1]);
        } else if (blockY < 20000 / BLOCK_SIZE && y % BLOCK_SIZE >= BLOCK_SIZE - 10) {
            /* 右チェック。右端以外のBlockでかつ、x座標をBLOCK_SIZEで割った時の余りがBLOCK_SIZEから半径の10を引いた値以上になる場合 */
            isStradding[3] = true;
            if (leftOfCoinSet[blockX][blockY + 1] == null) {
                leftOfCoinSet[blockX][blockY + 1] = new HashSet<Point>();
            }
            checkPointList.add(leftOfCoinSet[blockX][blockY + 1]);
        }
        if (isStradding[0] && isStradding[2]) {
            /* 左と下両方にはみ出ている場合のみ、追加で左下ブロックを判定 */
            if (thisCoin.getDistance(blockX * BLOCK_SIZE - 1, blockY * BLOCK_SIZE - 1) < 100) {
                if (leftOfCoinSet[blockX - 1][blockY - 1] == null) {
                    leftOfCoinSet[blockX - 1][blockY - 1] = new HashSet<Point>();
                }
                checkPointList.add(leftOfCoinSet[blockX - 1][blockY - 1]);
            }
        } else if (isStradding[0] && isStradding[3]) {
            /* 左と上両方にはみ出ている場合のみ、追加で左上ブロックを判定 */
            if (thisCoin.getDistance(blockX * BLOCK_SIZE - 1, (blockY + 1) * BLOCK_SIZE) < 100) {
                if (leftOfCoinSet[blockX - 1][blockY + 1] == null) {
                    leftOfCoinSet[blockX - 1][blockY + 1] = new HashSet<Point>();
                }
                checkPointList.add(leftOfCoinSet[blockX - 1][blockY + 1]);
            }
        } else if (isStradding[1] && isStradding[2]) {
            /* 右と下両方にはみ出ている場合のみ、追加で右下ブロックを判定 */
            if (thisCoin.getDistance((blockX + 1) * BLOCK_SIZE, blockY * BLOCK_SIZE - 1) < 100) {
                if (leftOfCoinSet[blockX + 1][blockY - 1] == null) {
                    leftOfCoinSet[blockX + 1][blockY - 1] = new HashSet<Point>();
                }
                checkPointList.add(leftOfCoinSet[blockX + 1][blockY - 1]);
            }
        } else if (isStradding[1] && isStradding[3]) {
            /* 右と上両方にはみ出ている場合のみ、追加で右上ブロックを判定 */
            if (thisCoin.getDistance((blockX + 1) * BLOCK_SIZE, (blockY + 1) * BLOCK_SIZE) < 100) {
                if (leftOfCoinSet[blockX + 1][blockY + 1] == null) {
                    leftOfCoinSet[blockX + 1][blockY + 1] = new HashSet<Point>();
                }
                checkPointList.add(leftOfCoinSet[blockX + 1][blockY + 1]);
            }
        }
        return checkDistance(thisCoin, checkPointList);
    }

    private static boolean checkDistance(Point point, List<Set<Point>> leftCoinSetList) {
        for (Set<Point> leftCoinSet : leftCoinSetList) {
            for (Point leftPoint : leftCoinSet) {
                if (leftPoint.getDistance(point) < 400) {
                    return false;
                }
            }
        }
        for (Set<Point> leftCoinSet : leftCoinSetList) {
            leftCoinSet.add(point);
        }
        /* 全てチェックOKならば、チェックした対象のSetに対して自分自身を追加する */
        return true;
    }

    static class Point {
        int x;
        int y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public boolean equals(Point other) {
            return x == other.x && y == other.y;
        }

        public int getDistance(int otherX, int otherY) {
            return (x - otherX) * (x - otherX) + (y - otherY) * (y - otherY);
        }

        public int getDistance(Point other) {
            return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
        }

    }
}
0