結果

問題 No.1265 Balloon Survival
ユーザー tenten
提出日時 2022-08-04 16:52:31
言語 Java
(openjdk 23)
結果
AC  
実行時間 535 ms / 2,000 ms
コード長 2,644 bytes
コンパイル時間 2,379 ms
コンパイル使用メモリ 79,088 KB
実行使用メモリ 69,704 KB
最終ジャッジ日時 2024-09-14 13:32:22
合計ジャッジ時間 11,741 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        Point[] points = new Point[n];
        for (int i = 0; i < n; i++) {
            points[i] = new Point(sc.nextInt(), sc.nextInt());
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                queue.add(new Path(i, j, points[i].getDistance(points[j])));
            }
        }
        HashSet<Integer> removed = new HashSet<>();
        int count = 0;
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (removed.contains(p.left) || removed.contains(p.right)) {
                continue;
            }
            if (p.left == 0) {
                removed.add(p.right);
                count++;
                continue;
            }
            removed.add(p.left);
            removed.add(p.right);
        }
        System.out.println(count);
    }
    
    static class Path implements Comparable<Path> {
        int left;
        int right;
        long value;
        
        public Path(int left, int right, long value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
    
    static class Point {
        long x;
        long y;
        
        public Point(long x, long y) {
            this.x = x;
            this.y = y;
        }
        
        public long getDistance(Point p) {
            return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0